在Laravel 5中进行动态导航的正确方法 [英] Proper way to make a dynamic navigation in Laravel 5
问题描述
我正在使用5.1构建我的第一个Laravel应用程序,这是一个电子商务网站.
I'm building my first Laravel application using 5.1, it's an ecommerce site.
我首先创建了静态"页面.我引用是因为页面不是从产品列表等动态生成的,但是仍从数据库中检索html.
I've started by creating the "static" pages. I quote because the pages are not dynamically generated from product lists etc, but the html is still retrieved from the database.
我已经创建了PagesController
,Page
模型,pages/index.blade.php
和pages/show.blade.php
视图以及MasterTemplate.blade.php
模板文件.
I've created a PagesController
, a Page
model, the pages/index.blade.php
and pages/show.blade.php
views, as well as a MasterTemplate.blade.php
template file.
我的routes.php
看起来像:
$router->get('/', [
'uses' => 'PagesController@index',
'as' => 'pages.index'
]);
$router->get('/{page}', [
'uses' => 'PagesController@show',
'as' => 'pages.show'
]);
这很好,我可以查看索引和数据库中的各个页面.
This works fine, I can view my index and individual pages that are in the DB.
我去添加导航时出现问题.由于我计划使用两个不同的导航栏(一个用于用户,一个用于管理员),所以我选择创建一个_navDefault.php
文件以包含在MasterTemplate
中.
My problem occurs when I go to add my navigation. Since I plan on using two different navigation bars (one for user, one for admins), I opted to create a _navDefault.php
file to include in the MasterTemplate
.
将多余的html剥离掉:
Stripping out the excess html it looks like:
@foreach ($pages as $page)
<li>{!! link_to_route('pages.show', $page->title, [$page->slug]) !!}</li>
@endforeach
这样可以很好地生成链接,并且它们可以正常工作.但是因为我的PagesController
:
This generates the links just fine and they work. But because my PagesController
:
...
public function index()
{
$pages = $this->page->get();
return view('pages.index', compact('pages'));
}
...
$pages
仅存在于index
视图上,因此导航到show
视图会给我一个错误,提示$pages
是未定义的,这是很合理的.
$pages
only exists on the index
view, and so navigating to a show
view gives me an error that $pages
is undefined, which makes perfect sense.
我可以在show
方法中定义$pages
,但是我还将拥有其他控制器,例如ProductController
和CartController
,它们将具有自己在导航栏中需要的页面,并且我可以最好在每个控制器的每个index
和show
方法中都包含$pages
,$products
和$cart
.
I could define $pages
in the show
method, but I will also have other controllers such as ProductController
and CartController
that will have their own pages I will need in the navigation bar, and I can't very well include $pages
, $products
, and $cart
in every index
and show
method for each controller.
我还是MVC的新手,所以我不确定使用导航控制器/模型或其他方法来处理此问题的最佳方法.
I'm still fairly new to MVC so I'm not sure of the best way to handle this, with a nav controller/model or something else.
使用多个控制器实现动态导航栏的正确方法是什么?
What is the proper way to achieve dynamic navigation bars using multiple controllers?
推荐答案
这是我能够在我的应用程序中随处放置动态导航栏的方式.
This is how I'm able to have my dynamic navbar everywhere in my app.
在AppServiceProvider
的boot
方法中测试以下内容:
Test the following within the boot
method of the AppServiceProvider
:
View::composer('*', function($view)
{
$view->with('pages', Page::all());
});
*
表示所有视图将收到$pages
.
您现在可以将其提取到专门用于查看作曲家的服务提供商.
You can now extract it to a Service Provider dedicated to view composers.
这篇关于在Laravel 5中进行动态导航的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!