Laravel - 控制器 - 动态菜单 - 所有页面
Laravel - Controller - Dynamic Menu - All Pages
我正在制作一个菜单可由管理员管理的网站,我的问题是如何安装 select 以显示在所有页面上。
一开始我是在控制器的所有动作中做菜单的查询,但是我想优化这个,就是不知道如何。
我的控制器。
<?php
namespace App\Http\Controllers;
use App\Categorias;
use Illuminate\Http\Request;
class FrontendController extends Controller {
public $template = 'default';
// Retorna Navegação
public function retornaNavegacao(){
$sql = Categorias::where([
['exibir', '=', 1], ['publicado', '=', 1]
])
->get();
return $sql;
}
// Página 'Categorias'
public function categoria(){
return view('frontend.'.$this->template.'.categorias.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
// Página 'Produtos'
public function produto(){
return view('frontend.'.$this->template.'.produtos.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
// Página 'Contato'
public function contato(){
return view('frontend.'.$this->template.'.contato.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
}
函数retornaNavegacao()只查询数据库和returns所有注册的菜单(有关系可以搜索所有子类目)
但是我必须在控制器的所有操作中重复菜单代码,我认为它可能有更聪明的方法,不必在所有操作中重复此代码。
您可以通过应用服务提供商的启动方法在您的所有视图中共享一个变量。
public function boot()
{
$mainMenu = $this->retornaNavegacao(),
view()->share(compact('mainMenu'));
}
如果您只想在某些特定视图中共享变量,您可以创建自己的 view composer。
在AppServiceProvider
class中编辑boot
方法
public function boot()
{
$categories = Categorias::where([
['exibir', '=', 1],
['publicado', '=', 1]
])
->get();
View::composer('*', function ($view) {
$view->with(['mainMenu' => 'categories']);
});
}
您还可以使用 laravel
的所有力量
public function boot()
{
$mainMenu = Categorias::whereWxibir(1)
->wherePublicado(1)
->get();
View::composer('*', function ($view) {
$view->with(compact('mainMenu'));
});
}
我正在制作一个菜单可由管理员管理的网站,我的问题是如何安装 select 以显示在所有页面上。
一开始我是在控制器的所有动作中做菜单的查询,但是我想优化这个,就是不知道如何。
我的控制器。
<?php
namespace App\Http\Controllers;
use App\Categorias;
use Illuminate\Http\Request;
class FrontendController extends Controller {
public $template = 'default';
// Retorna Navegação
public function retornaNavegacao(){
$sql = Categorias::where([
['exibir', '=', 1], ['publicado', '=', 1]
])
->get();
return $sql;
}
// Página 'Categorias'
public function categoria(){
return view('frontend.'.$this->template.'.categorias.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
// Página 'Produtos'
public function produto(){
return view('frontend.'.$this->template.'.produtos.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
// Página 'Contato'
public function contato(){
return view('frontend.'.$this->template.'.contato.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
}
函数retornaNavegacao()只查询数据库和returns所有注册的菜单(有关系可以搜索所有子类目)
但是我必须在控制器的所有操作中重复菜单代码,我认为它可能有更聪明的方法,不必在所有操作中重复此代码。
您可以通过应用服务提供商的启动方法在您的所有视图中共享一个变量。
public function boot()
{
$mainMenu = $this->retornaNavegacao(),
view()->share(compact('mainMenu'));
}
如果您只想在某些特定视图中共享变量,您可以创建自己的 view composer。
在AppServiceProvider
class中编辑boot
方法
public function boot()
{
$categories = Categorias::where([
['exibir', '=', 1],
['publicado', '=', 1]
])
->get();
View::composer('*', function ($view) {
$view->with(['mainMenu' => 'categories']);
});
}
您还可以使用 laravel
的所有力量public function boot()
{
$mainMenu = Categorias::whereWxibir(1)
->wherePublicado(1)
->get();
View::composer('*', function ($view) {
$view->with(compact('mainMenu'));
});
}