[英]Laravel 5.2 'Master Layout' Variable passing
So here is what I'm trying to achieve. 所以这就是我想要达到的目标。 I have a default blade template default.blade.php
which is extended by all of my child views. 我有一个默认的刀片服务器模板default.blade.php
,所有我的子视图都对其进行了扩展。
Within my default.blade.php
i have a foreach loop which expresses some 'global' options to the user, and example of which is below. 在我的default.blade.php
我有一个foreach循环,该循环向用户表示一些“全局”选项,下面是其中的示例。
@foreach ($projects as $p)
<li><a href='$p->project_uid'>{{ $p->project_name }}</a></li>
@endforeach
So to achieve this I'm having to pass the $projects
variable via the view('viewname', $data)
or via View::share('projects', $projects);
因此,要实现此目的,我必须通过view('viewname', $data)
或通过View::share('projects', $projects);
传递$projects
变量View::share('projects', $projects);
in my Controller __construct()
在我的控制器__construct()
Is there a better way for me to do this on a global sense so that the above calls don't need to be made? 我是否有更好的方法从全局角度进行此操作,从而无需进行上述调用?
One option i am aware of is calling a Model function from within my view, but this defies the concept of MVC so is not ideal. 我知道的一种选择是从我的观点内调用Model函数,但这违背了MVC的概念,因此并不理想。
Any guidance on the subject would be appreciated, Thanks. 谢谢您对此主题的任何指导。
So i tried the ViewComposer solution but ran into a couple of problems. 所以我尝试了ViewComposer解决方案,但遇到了两个问题。 Below is my Composer & the Service Provider Register. 以下是我的作曲家和服务提供商注册。
Config/app.php 配置/ app.php
App\Providers\ComposerServiceProvider::class,
ComposerServiceProvider ComposerServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
/**
* Register bindings in the container.
*
* @return void
*/
public function boot() {
// Using class based composers...
view ()->composer ( 'default', 'App\Http\ViewComposers\MasterComposer' );
}
/**
* Register the service provider.
*
* @return void
*/
public function register() {
//
}
}
MasterComposer MasterComposer
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use App\Repositories\UserRepository;
use Sentinel;
use ProjectUsers;
class MasterComposer
{
protected $users;
public function __construct(ProjectUsers $users)
{
$uid = Sentinel::getUser()->id;
$this->users = ProjectUsers::where("user_id", '=', $uid)->get();
}
public function compose(View $view)
{
$view->with('projects', $this->users);
}
}
Am i missing something obvious as it doesn't seem like the Composer is being loaded at all. 我是否缺少明显的东西,因为似乎完全没有加载Composer。
Fixed it myself. 自己修复。 Realised that within the ComposerServiceProvider i need to specify a full path to my view like so. 意识到在ComposerServiceProvider中,我需要像这样为我的视图指定完整路径。
view ()->composer ( 'admin/layouts/default', 'App\Http\ViewComposers\MasterComposer' );
Now it Works :D 现在可以使用了:D
Yes, you do this with View Composer . 是的,您可以使用View Composer进行此操作。
View composers are callbacks or class methods that are called when a view is rendered. 视图编写器是在呈现视图时调用的回调或类方法。 If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location. 如果每次渲染视图时都有要绑定到视图的数据,则视图编辑器可以帮助您将逻辑组织到一个位置。
Bind that data to defualt.blade.php
view, like: 将该数据绑定到defualt.blade.php
视图,例如:
public function compose(View $view)
{
$data = .... // Get data here.
$view->with('projects', $data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.