Rai*_*aan 5 php package laravel laravel-5
我做了一个包,它计算网页上的访问者。目前我只有一个路由、控制器和视图,除了显示一个简单的字符串外,它没有做太多事情。我有一个单独的 Laravel 应用程序,这个包是专门为它构建的。在这个单独的应用程序中,我有一个名为 backend 的布局文件。
layouts/layouts/backend.blade.php.
我的包视图像这样扩展这个模板:(backend.blade.php 不存在于包中,但在单独的 Laravel 应用程序中)
@extends('layouts.layouts.backend')
@section('content')
<div class="container-fluid pt-5 ">
<div class="row">
<div class="col-md-6">
<h3>{{ __('Visitors') }}</h3>
</div>
</div>
</div>
@endsection
Run Code Online (Sandbox Code Playgroud)
该包成功扩展了此布局,但找不到诸如 之类的功能Auth::user()->token,它会说
Trying to get property 'token' of non-object (View: /Users/rainierlaan/Sites/rainierlaan/resources/views/layouts/layouts/backend.blade.php)
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?
这是我的包裹服务提供商
public function register()
{
// Controllers
$this->app->make('Rainieren\Visitors\Http\Controllers\VisitorController');
// Views
$this->loadViewsFrom(__DIR__.'/resources/views', 'visitors');
$this->publishes([
__DIR__.'/resources/views' => resource_path('views/visitors'),
]);
// Migrations
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
include __DIR__ . '/routes/routes.php';
}
Run Code Online (Sandbox Code Playgroud)
当我执行vendor:publish视图成功发布到正确的文件夹但不知何故无法识别功能,如Auth::user()->token或Auth::user()->unreadNotifications->count())
这是我的包裹路线:
<?php
Route::get('dashboard/visitors', '\Rainieren\Visitors\Http\Controllers\VisitorController@index')->name('visitors');
Run Code Online (Sandbox Code Playgroud)
这是控制器
public function index()
{
return view('visitors::index');
}
Run Code Online (Sandbox Code Playgroud)
我需要更多调试信息,但我的第一个假设是缺少中间件AuthenticateSession或Authenticate中间件。
Laravel 定义了一个默认的中间件组 web为你的路由routes/web.php,这个组使用AuthenticateSession中间件。这是新安装的样子:
Route::group([
'middleware' => 'web', <<< this is the magic part
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
Run Code Online (Sandbox Code Playgroud)
这里我们看到使用了中间件组web。
在您的自定义模块/服务提供商中,情况并非如此。您的Route::get()定义已添加到Router该组中,但不在该组内。因此,不会执行所有必要的内部验证来验证用户。
在这种情况下,我会尝试使用->middleware('auth')or ->middleware('web')which 将使用 main-projects 组中间件。
Route::get('dashboard/visitors', '\Rainieren\Visitors\Http\Controllers\VisitorController@index')
->name('visitors')
->middleware('web');
Run Code Online (Sandbox Code Playgroud)
这是一个不同的想法:
如果你说你总是经过身份验证。然后,您可以尝试将所有web中间件移动到Kernel( protected $middleware = []) 中的全局中间件中。
我没有测试过这个,但我可以想象这也可以工作。