继指引,我有扩展核心和显示数据在每一页Laravel
1)创建一个服务提供商
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
view()->composer('template', function ($view) {
$view->with('series_list', Series::all());
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
2)注册商
App\Providers\ViewComposerServiceProvider::class,
3 )并在模板处回显变量
{!! var_dump($series_list) !!}
的问题是:
Route::get('/', '[email protected]');
Route::get('product/view/{id}', '[email protected]');
Route::get('product/detail/{id}', '[email protected]');
Route::get('/page/contact', '[email protected]');
的PageController和HomeController中可以显示$ series_list,但ProductController的将返回错误:
Undefined variable: series_list in the template
这里是ProductController的:
class ProductController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function view($id = 1)
{
return view('product/list');
}
public function detail($id = 1)
{
return view('product/detail');
}
}
非常感谢帮助。
你能回家和产品/列表视图? –