Laravel:传递默认变量以查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17645087/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:08:24  来源:igfitidea点击:

Laravel: Passing default variables to view

phplaravel

提问by enchance

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

在 Laravel 中,我们都以几乎相同的方式将数据传递给我们的视图

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

But is there some way to add default variables to the view without having to declare it over and over in $data? This would be very helpful for repeating variables such as usernames, PHP logic, and even CSS styles if the site demands it.

但是有什么方法可以将默认变量添加到视图中,而不必在$data. 如果站点需要,这对于重复变量(例如用户名、PHP 逻辑甚至 CSS 样式)非常有帮助。

回答by The Alpha

Use View Composers

使用 View Composer

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:

现在,每次创建配置文件视图时,计数数据都将绑定到视图。在您的情况下,它可能用于id

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

So the $idwill be available to your myawesomeviewview each time you create the view using :

因此,每次您使用以下命令创建视图时$id,您的myawesomeview视图都可以使用:

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:

如果您更愿意使用基于类的 Composer,这将提供通过应用程序IoC Container解决的好处,您可以这样做:

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

A view composer class should be defined like so:

视图 Composer 类应该像这样定义:

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

Documentationand you can read this articletoo.

文档,您也可以阅读本文

回答by windmaomao

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

有几种方法,到目前为止我已经尝试了一些。

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

1.使用singleton,可以放在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.

2.使用设置包,在这里下载。https://github.com/Phil-F/Setting。你可以把它放在控制器或模板中。

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

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

3.使用视图共享,几乎在你的模板中使用它

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.

4.我当前的示例设置,我在 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', '');
}

回答by Slack Undertow

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

@enchance,作为使用“*”的替代方法,如您的评论中所述,也许 View::share 也会对您有所帮助。从 Laravel 文档:

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

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

您还可以在所有视图中共享一条数据:

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

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

摘录来自http://laravel.com/docs/responses

回答by fideloper

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.

您可以使用它向一个视图或一组视图添加数据。