我有以下的web.php代碼Laravel中間件不laravel工作5.4
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
class CheckLogin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(!Session::has('userAuthDetail')){
// return redirect('login');
// return redirect()->guest('/login');
} else {
return $next($request);
}
}
}
Route::get('/login', ['as' => 'login', 'uses' => 'Auth\[email protected]']);
Route::group(['middleware' => ['login']], function() {
Route::post('/add','[email protected]');
});
登錄控制器
public function postLogin(Request $request) {
// print_r($request->all());
$data['email']=$request->email;
$data['password']=$request->password;
$response= ApiModel::userLogin($data);
if($response->msg=='success'){
session(['userAuthDetail' => $response->data[0]]);
return redirect('/');
} else if($response->msg=='failed'){
return redirect('/');
}
}
如果我添加如果中間件其他條件,那麼網站投擲錯誤
ErrorException在VerifyCsrfToken.php行156:試圖如果我刪除的if else並添加只是
public function handle($request, Closure $next) { if(!Session::has('userAuthDetail')){ } return $next($request); }
則做工精細獲得非對象
的財產 但如果再失敗的憑據其重定向到儀表板。
注:我不能使用laravel權威性,因爲我與這是寫在PHP核心的API通信
更新;
kernal.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckLogin::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'login' => \App\Http\Middleware\CheckLogin::class,
];
}
你有沒有在中間件中定義poslogin函數路由? – kunal
@kunal .no其登錄中間路線外 – vision
您是如何在Kernel.php文件中註冊中間件的? –