【问题标题】:Laravel 5.2 - Common OBJECT to all controllers and viewsLaravel 5.2 - 所有控制器和视图的通用对象
【发布时间】:2016-07-13 22:47:09
【问题描述】:

我是 Laravel 的新手,目前正在编写一个 Intranet 应用程序,它基本上是一个包含大量信息的仪表板,使用 Laravel 5.2,其中一个要求是能够浏览不同的商店。应用程序的每个页面,都需要商店的代码。

目前我的所有表中都有一个列 store_id,我使用带有路由的 GET 来检索这个值,例如:

www.mysite.com/1/employees -> will list all employees from Store 1
www.mysite.com/1/employees/create -> will create employee to Store 1
www.mysite.com/2/financial -> will list all financial widgets with data from Store 2

我想从 GET 中删除我的 STORE_ID,并对我的 topbar.blade.php 中的所有商店使用 DROPDOWN 选择,例如:

<select>
  <option selected>Store1</option>
  <option>Store2</option>
</select>

每当有人选择“Store1”或“Store2”时,我都想使用 StoreController 获取 Store 信息,并使此变量可用于所有控制器和视图。我可以在哪里使用以下网址

www.mysite.com/employees -> will list all employees from "Depending of the SELECT"
www.mysite.com/employees/create -> will create employee to "Depending of the SELECT"
www.mysite.com/financial -> will list all financial widgets with data from "Depending of the SELECT"

我已经阅读了 View Composer、Facades、ServiceProvide,但我对它们都感到非常困惑。

【问题讨论】:

    标签: controller views laravel-5.2 intranet laravel-facade


    【解决方案1】:

    您还可以共享来自提供商的公共数据,例如。 AppServiceProvider 或您自己的提供商。 例如,我在这里使用AppServiceProvider。 在AppServiceProvider启动方法中:

    public function boot()
    {
        $this->passCommonDataToEverywhere();
    }
    

    现在写在方法里:

    protected function passCommonDataToEverywhere()
    {
        // Share settings
        $this->app->singleton('settings', function() {
            return Setting::first();
        });
        view()->share('settings', app('settings'));
    
        // Share languages
        $this->app->singleton('languages', function() {
            return Language::orderBy('weight', 'asc')->get();
        });
        view()->share('languages', app('languages'));
    }
    

    在这个例子中我必须使用:

    use App\Language;
    use App\Setting;
    

    【讨论】:

    • 您好,感谢您的回答。每当我在 top.blade.php 中选择一个商店时,如何使用变量调用这个 ServideProvider ?公共函数 boot() { $this->ShareStore(); } protected function passCommonDataToEverywhere($id) { // 共享设置 $this->app->singleton('store', function() { return Store::where('id',$id); }); view()->share('store', app('store')); }
    • public function boot (Router $router) { $router-&gt;bind('id',function($id) { $this-&gt;ShareStore($id); }); } 但我认为你应该小心使用它,因为它现在依赖于 {$id} 参数
    【解决方案2】:

    真的没那么难。可能还有其他方法,但我更喜欢这样做:

    共享数据:

    打开app/Http/Controllers/Controller.php 并添加如下构造函数:

    <?php
    
    namespace App\Http\Controllers;
    
    ...
    
    abstract class Controller extends BaseController
    {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
        public function __construct()
        {
            $this->sharedVar = "I am shared.."; // to share across controllers
            view()->share('sharedVar',$this->sharedVar); // to share across views
        }
    }
    

    使用数据:

    1.在控制器中:

    所有控制器都扩展了上述控制器。所以该属性对所有控制器都可用:

    class YourController extends Controller
    {
        public function index()
        {
            dd($this->sharedVar);
        }
    ...
    }
    

    2。在视图中:

    {{$sharedVar}} // your-view.blade.php
    

    编辑:

    如果您想将数据共享到控制器和视图以外的地方,最好的方法可能是使用AppServiceProvider

    打开app/Providers/AppServiceProvider.php并更新boot()方法:

    class AppServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            $this->app->singleton('sharedVariable', function () {
                return "I am shared";
            });
        }
    
        ...
    
    }
    

    用法:

    dd(app('sharedVariable')); // anywhere in the application
    

    【讨论】:

    • 我喜欢这个,但唯一的“问题”(至少对我来说,呵呵)是使用插件 Widgets,它扩展了 AbstractWidget。那里也有吗?
    • 太好了,非常感谢。所以它让我想到另一个问题。例如,每当有人选择 Store 1 时,我将调用 StoreController 作为 POST/GET 来检索该 Store 中的所有数据,我如何动态地共享这些信息?如何将对象作为参数传递给 ServiceProvider ?
    • 我可以在 StoreController 中做这样的事情吗? app('sharedVariable') = '你好';
    【解决方案3】:

    我想知道以下是否可能:

    -- StoreController
    
    public function BindStore($id)
    {
        $store = Store::where('id', $id);
        App::singleton('store', $store);
    }
    

    或者也许使用服务

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2017-03-31
      相关资源
      最近更新 更多