【问题标题】:How can I pass a variable to every view in Laravel without having to pass it from every controller?如何将变量传递给 Laravel 中的每个视图,而不必从每个控制器传递它?
【发布时间】:2020-07-01 13:10:54
【问题描述】:

我正在尝试让通知徽章显示用户拥有的未读消息的数量。以下作品:

// Controller
public function messages() 
{
    $messages = MessagesController::getMessages();
    $newNotificationNumber = MessagesController::getNumberOfNewMessages();

    return view('pages.messages', compact('messages'), compact('newNotificationNumber'));
}

我的app.blade.php 文件的结构如下:

// html stuff
@include('layouts.navbar')
<main class="py-4 bg-white">
  <div class="container">
    @yield('content')
  </div>
</main>

导航栏显示数字如下:

<span class="badge badge-pill badge-primary">{{ $newNotificationNumber ?? '' }}</span>

如果我在每个控制器函数中都包含compact('newNotificationNumber'),我的通知就会像我想要的那样工作,但这很乏味并且容易出错。有什么建议?

【问题讨论】:

标签: php laravel bootstrap-4 laravel-blade


【解决方案1】:

你可以创建一个新的控制器,比如说AppController,它将扩展 Laravel 的默认 App\Http\Controllers 控制器。在这个新控制器中,使用您需要的所有数据创建您的构造函数,并将它们share 发送到所有视图:

public function __construct(Request $request)
{
    $messages = MessagesController::getMessages();
    $newNotificationNumber = MessagesController::getNumberOfNewMessages();
    View::share('languages', $languages);
    View::share('newNotificationNumber', $newNotificationNumber);
}

之后,您可以在需要变量的每个其他控制器中扩展 AppController

class YourController extends AppController

现在剩下要做的就是在YourController 中扩展AppController 构造函数:

public function __construct()
{
    parent::__construct();
}

这样,您将可以访问您在YourController 中使用的所有视图中的$languages$newNotificationNumber 变量。

【讨论】:

    【解决方案2】:

    我会推荐您使用服务提供商的最佳方式。您可以将 AppServiceProvider.php 位于 app\Providers 或创建一个新的服务提供者(我强烈推荐);

    首先让我们创建一个新的 Provider 调用 MasterViewServicerProvider

    *php artisan make:provider MasterViewServicerProvider
    
    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Facades\View; /** Let's Import View Facade **/
    use App\Model\CAS\Message; /** Let's Import the our Model **/
    
    class MasterViewServicerProvider extends ServiceProvider
    {
    
    public function register()
    {
        //
    }
    
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
    
    /** First we have to check if the table exist in our database, the reason is that if we try to run migration and the table doesn't Message doesn't exist, it will through an error because the provider will render before the migration **/
    
        if(\Schema::hasTable('messages'))
        {
          $message= Message::all();
          /** message = Message::get(['title','id']); // get specific column instead of all record on the table  **/
          View::share('message', $message);
        }
    }
    

    }

    现在让我们在 conf\app.php 中注册我们的 Provider

    'providers' => [
        .......
        App\Providers\MasterViewServicerProvider::class,
    ],
    

    现在您可以在应用程序的任何位置调用 $message 变量。希望能有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2018-04-24
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      相关资源
      最近更新 更多