Laravel: Passing default variables to view

前端 未结 4 1021
遇见更好的自我
遇见更好的自我 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 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: 
    

    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', '');
    }
    

提交回复
热议问题