我创建了一个laravel软件包,并添加了路线,视图,迁移等,并将其链接到我的项目。到目前为止,一切都是正确的,但是身份验证不起作用,我唯一要做的就是像在正常的laravel项目中那样生成身份验证路由,并且我已通过以下方式将其添加到路由组中:
<?php
use Illuminate\http\Request;
//That file is the package's web.php
Route::group(['namespace'=>'myPackage\Blog\Http\Controllers', 'middleware' => 'auth'], function(){
Route::get('blog','BlogController@index')->name('blog');
Route::post('blog', 'BlogController@send');
Route::get('administrador', 'adminController@index')->name('administrador');
});
这就是我包裹的serviceProvider:
<?php
namespace Tetres\Blog;
use Illuminate\Support\ServiceProvider;
class BlogServiceProvider extends ServiceProvider{
public function boot()
{
$this->loadRoutesFrom(__DIR__.'/routes/web.php');
$this->loadViewsFrom(__DIR__.'/views', 'blog');
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
}
public function register()
{
}
}
只需添加'middleware' => 'auth'
,这些路线就会停止工作,我应该怎么做?谢谢!
答案 0 :(得分:1)
我面临着同样的问题。在@lagbox上展开
在通常指示$this->middleware("web");
的位置旁边添加$this->middleware("auth");
如何在控制器的__construct()
中执行此操作的示例。
class my_controller extends Controller
{
public function __construct()
{
$this->middleware("web"); // this will solve your problem
$this->middleware("auth");
}
...
}
以您为例,将'web'
添加到中间件:
//That file is the package's web.php
Route::group(['namespace'=>'myPackage\Blog\Http\Controllers', 'middleware' => ['web', 'auth']], function(){
Route::get('blog','BlogController@index')->name('blog');
Route::post('blog', 'BlogController@send');
Route::get('administrador', 'adminController@index')->name('administrador');
});