简体   繁体   English

Laravel基本控制器方法

[英]Laravel base controller methods

I'm learning how to use Laravel by writing a little test application. 我正在通过编写一些测试应用程序来学习如何使用Laravel。 So far its going really well, and I'm excited by all the features of the framework. 到目前为止,它的运行情况非常好,我对该框架的所有功能感到兴奋。 However, I keep hitting a wall when I try to apply a specific design pattern. 但是,当我尝试应用特定的设计模式时,我总是碰壁。

I've currently got a set up where I can set the html titles, keywords and description with default values in the base controller, and then optionally change them in the child controller that extends it: 目前,我已经进行了设置,可以在基本控制器中使用默认值设置html标题,关键字和描述,然后在扩展它的子控制器中有选择地更改它们:

Base controller 基本控制器

protected function setupLayout()
{
    $this->layout->title = 'Wow website';
    $this->layout->desc = 'Much technical';
    $this->layout->keys = 'so html, very javascript';

    $foot = View::make('mod.footerDefault');
    $this->layout->footer = $foot;
}

Child controller 子控制器

public function getContact()
{
    $this->title = 'Contact page of contactness';
    // Could also override the desc and keys if desired,
    // but I'll just let them default here

    $data = 'wow';
    $view = View::make('main.home', $data)
        ->nest('vid', 'mod.video')
        ->nest('list', 'mod.list');

    $this->layout->content = $view;
}

This is all gravy, but what if I have an object that I've interacted with, such as a menu class, that needs to be recompiled into a string of HTML? 这全是肉汁,但是如果我有一个与之交互的对象(例如菜单类)需要重新编译为HTML字符串怎么办? Is there a method in laravel that is similar to the setupLayout() method which is called automatically, but each time the View::make method is invoked? laravel中是否有一个类似于setupLayout()方法的方法,该方法会自动调用,但是每次调用View :: make方法时都可以?

(More examples...just to illustrate) (更多示例...只是为了说明)

Base controller 基本控制器

protected function setupLayout()
{
     $this->menu = newMenu();
     $this->menu->items[0] = ['contact-us', 'Contact us'];
     $this->menu->items[1] = ['about-us', 'About us'];
     $this->menuString = $this->menu->getString();
}

Child controller 子控制器

public function getContact()
{
    $this->menu->addItem(['log-out', 'Log out']);
    // Now $this->menuString must be recalculated.
    // Ideally want to avoid having to call a method from the base controller
    // every time the child controller calls View::make

    $view = View::make('main.home')
    $this->layout->content = $view;
}

I know there is such a thing as view composers, but I dont think these are called on View::make(). 我知道有诸如View Composers之类的东西,但我不认为这些是在View :: make()上调用的。

I create my navigation/menu structure with a view composer. 我使用视图编辑器创建导航/菜单结构。

  • Create a view composer providing the data for the view 创建一个视图编辑器,为视图提供数据
  • Create a view for your navigation part ( view/parts/navigation.blade.php ), containing only the navigation created from the data provided by your view composer 为您的导航部件创建一个视图( view/parts/navigation.blade.php ),仅包含根据视图编辑器提供的数据创建的导航
  • Bind that composer to your navigation view with View::composer(your_view, your_composer) 使用View::composer(your_view, your_composer)将该作曲家绑定到您的导航视图
  • include the navigation view into any other view or master layout with @include('parts.navigation') 使用@include('parts.navigation')将导航视图包含到任何其他视图或主布局中

This composer function is not called on every View::make() but on every make of a View that contains the included navigation part. 不是在每个View::make()上调用此composer函数,而是在包含所包含的导航部分的View的每个make上调用。 And this is what you need. 这就是您所需要的。

Here is an how to how view composers work: http://culttt.com/2014/02/10/using-view-composers-laravel-4/ 这是视图作曲家的工作方式: http : //culttt.com/2014/02/10/using-view-composers-laravel-4/

Heres how I maanged to get it working in the end: 以下是我如何使它最终运行的方法:

The routes file (Will move this to a more appropriate place later): 路由文件 (稍后将其移至更合适的位置):

    // NB: KoBsMenu is the name of the menu class

    App::before(function($request) {
        $menu = [
            'brand' => [
                'left' => 'asdf',
                'right' => ['floppy-save']
            ],
            'leftItems' => [
                'home' => [1, 'home', 'Home'],
                'factories' => [2, [
                    'aboutF' => [1, 'about-factories', [''], 'About factories'],
                    'factoryL' => [2, 'factory-life', 'Factory life'],
                    'uk' => [3, [
                        'leeds' => [1, 'leeds', 'Leeds'],
                        'reading' => [2, 'reading', 'Reading']
                    ], 'UK'],
                    'usa' => [4, [
                        'austin' => [1, 'austin', 'Austin'],
                        'kansas' => [2, 'kansas', 'Kansas']
                    ], 'USA']
                ], 'Factories', ['tower']],
            ...
        ];

        App::singleton('mainMenu', 'KoBsMenu');
        $KoBsMenu = App::make('mainMenu');
        $KoBsMenu->buildMenu($menu);
    });
    View::composer('*', function($view)
    {
        $KoBsMenu = App::make('mainMenu');
        $mainMenu = $KoBsMenu->getMenuHtml();
        $view->with('mainMenu', $mainMenu);
    });

Then in the base controller 然后在基本控制器中

$this->KoBsMenu = App::make('mainMenu');

Then I can add/remove menu items programatically in the main controller if desired. 然后,可以根据需要在主控制器中以编程方式添加/删除菜单项。

$this->KoBsMenu->menu['leftItems']['testasfd'] = [1, 'bloh', [''], 'Bloh']; // Will be a public method ultimately

Then finally in the view: 然后终于在视图中:

{{ $mainMenu or '' }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM