laravel share variable across all methods in a controller

人走茶凉 提交于 2019-12-24 07:15:52

问题


i am making a simple website in PHP laravel framework where the top navigation links are being generated dynamically from the database. I am generating the $pages variable in the home controller action and passing to layout file. My code is as below:

 public function home()
{
    $pages = Page::all();
    return View::make('home')->with('pages', $pages);
}

public function login()
{
    return View::make('login');
}

But when i try to access the login action, i get the error variable $pages not found since the $pages variable is being accessed in layout file. How can i share the same variable across all the actions in a controller?


回答1:


I think a fairly straightforward way to do this is by using the controller's constructor. It's sometimes helpful to be able to see the vars available to all methods in a controller from within that controller, instead of hidden away in a service provider somewhere.

class MyController extends BaseController
{
    public function __construct()
    {
        view()->share('sharedVar', 'some value');
    }

    public function myTestAction()
    {
        view('view.name.here');
    }
}

And in the view:

<p>{{ $sharedVar }}</p>



回答2:


You can use singleton like the following

App::singleton('data', function() { return array('abc' => 1); });

This way, you can call it anywhere in your controller or template like

$data = App::make('data');

Following this, you could try using a bundle developed by Phil here, https://github.com/Phil-F/Setting. Once installed, then you can just refer it in controller or template by

Setting::get('title')

Of course, you can set it anywhere by using

Setting::set('title', 'Portfolio');

Setting allows you store them both in cache and json file which might be another way to get the value back.




回答3:


I solved the problem by using Laravel's view composer. I made a header.blade.php and passed the $pages variable to it and added following code to my routes.php file.

View::composer('header', function($view){
   $pages = Page::all();
   $view->with('pages', $pages);
});   



回答4:


You forgot to add as a parameter inside your login action.

public function login()
{
    $pages = Page::all();
    return View::make('login')->with('pages',$pages);
}

My older reply:

To share the same variable across all the actions in a controller, raw PHP should help you out:

<?php

Class MyController Extends BaseController {
    var $pages = Page::all();

     public function home()
    {
        return View::make('home')->with('pages', $this->pages);
    }

    public function login()
    {
        $pages = $this->pages;
        return View::make('login');
    }
}

But personally, I would prefer making a variable in model, something like:

<?php

Class Page extends Eloquent {

    public static $all_ages = Page::all();
}

And access it like:

Page::$all_pages;


来源:https://stackoverflow.com/questions/18722033/laravel-share-variable-across-all-methods-in-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!