laravel 在控制器中的所有方法之间共享变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18722033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel share variable across all methods in a controller
提问by imran
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:
我正在 PHP laravel 框架中制作一个简单的网站,其中顶部导航链接是从数据库动态生成的。我在主控制器操作中生成 $pages 变量并传递给布局文件。我的代码如下:
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?
但是当我尝试访问登录操作时,我收到错误变量 $pages not found 因为 $pages 变量在布局文件中被访问。如何在控制器中的所有操作中共享相同的变量?
回答by omarjebari
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>
回答by windmaomao
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
在此之后,您可以尝试使用 Phil 在这里开发的包,https://github.com/Phil-F/Setting。安装后,您可以通过以下方式在控制器或模板中引用它
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.
设置允许您将它们存储在缓存和 json 文件中,这可能是另一种取回值的方法。
回答by imran
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.
我通过使用 Laravel 的视图作曲家解决了这个问题。我创建了一个 header.blade.php 并将 $pages 变量传递给它,并将以下代码添加到我的 routes.php 文件中。
View::composer('header', function($view){
$pages = Page::all();
$view->with('pages', $pages);
});
回答by Arda
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 应该可以帮助您:
<?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;