Laravel: Passing default variables to view

前端 未结 4 1010
遇见更好的自我
遇见更好的自我 2020-12-29 00:02

In Laravel, we all pass data to our view in pretty much the same way

$data = array(
    \'thundercats\' => \'Hoooooooooooh!\'
);
return View::make(\'myawe         


        
相关标签:
4条回答
  • 2020-12-29 00:45

    Yep there absolutely is a way - see here on view composers.

    You can use that to add data to a view or set of views.

    0 讨论(0)
  • 2020-12-29 00:50

    Use View Composers

    View composers are callbacks or class methods that are called when a view is created. If you have data that you want bound to a given view each time that view is created throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".

    Defining A View Composer :

    View::composer('profile', function($view)
    {
        $view->with('count', User::count());
    });
    

    Now each time the profile view is created, the count data will be bound to the view. In your case, it could be for id :

        View::composer('myawesomeview', function($view)
        {
            $view->with('id', 'someId');
        });
    

    So the $id will be available to your myawesomeview view each time you create the view using :

    View::make('myawesomeview', $data);
    

    You may also attach a view composer to multiple views at once:

    View::composer(array('profile','dashboard'), function($view)
    {
        $view->with('count', User::count());
    });
    

    If you would rather use a class based composer, which will provide the benefits of being resolved through the application IoC Container, you may do so:

    View::composer('profile', 'ProfileComposer');
    

    A view composer class should be defined like so:

    class ProfileComposer {
        public function compose($view)
        {
            $view->with('count', User::count());
        }
    }
    

    Documentation and you can read this article too.

    0 讨论(0)
  • 2020-12-29 00:54

    @enchance, as an alternative to using '*', as mentioned in your comment, perhaps a View::share would help you too. From the Laravel documentation:

    You may also share a piece of data across all views:

    View::share('name', 'Steve');

    Excerpt is from http://laravel.com/docs/responses

    0 讨论(0)
  • 2020-12-29 01:00

    There are couple of ways, so far I have been experiment with some.

    1.Use singleton, you can put it in routes.php

    App::singleton('blog_tags', function() {
      return array(
        'Drupal'    => 'success',
            'Laravel'   => 'danger',
            'Symfony'   => 'dark',
            'Wordpress' => 'info'
        );
    });
    

    2.Use Settings bundle, download here. https://github.com/Phil-F/Setting. You can put this in controller or template.

    Setting::set('title', 'Scheduler | Mathnasium');
    

    3.Use View share, pretty much use it in your template

    Controller: Views::share('theme_path', 'views/admin/');
    Template: <link href="{{ $theme_path }}/assets/bootstrap.min.css"/>
    

    4.My current sample setup, I wrote a construct in HomeController.

    public function __construct()
    {
        // Define a theme namespace folder under public
        View::addLocation('../public/views/admin');
        View::addNamespace('admin', '../public/views/admin');
        View::share('theme_path', 'views/admin/');
    
    
        // Set default page title
        Setting::set('title', 'Scheduler | Mathnasium');
        Setting::set('description', 'daily customer scheduler.');
        Setting::set('keywords', ['Reservation', 'Planner']);
        Setting::set('page-title', '');
    }
    
    0 讨论(0)
提交回复
热议问题