我有简单的控制器:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class InboxController extends Controller
{
public function compose(Request $request)
{
if($request->isMethod('post'))
{
$rules = [
'to' => 'required|email',
'from' => 'required|email',
'subject' => 'required|max:10',
'message' => 'required'
];
$this->validate($request, $rules);
}
$array = array('messages'=>$this->user_messages(), 'count' => $this->count_messages());
return view('compose', $array);
}
}
如果我想在所有路由中验证已发送数据,则会出错:
Session store not set on request.
如何在Laravel 5.5中根据请求设置会话以验证数据?
我必须验证compose
路线
Routes
Route::group(['prefix' => 'inbox', 'middleware' => 'auth'], function(){
Route::get('/', ['uses' => 'InboxController@index',])->name('inbox');
Route::match(['get', 'post'], '/message/{id?}', ['uses'=>'InboxController@message'])->name('message');
Route::match(['get', 'post'], '/compose', ['uses'=>'InboxController@compose'])->name('compose');
});
我的中间件组代码:
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,
],
答案 0 :(得分:0)
您需要添加
\Illuminate\Session\Middleware\StartSession::class
在kernal.php网络组内的中间件中。
答案 1 :(得分:0)
如果您需要会话状态,CSRF保护等,您将需要使用Web中间件。
Route::group(['middleware' => ['web']], function () {
// your routes here
});
答案 2 :(得分:0)
这是因为
在Kernal.php中(位置-> / app / Http)
有两个数组 $ middleware 和 $ routeMiddleware 。
请勿在$ middleware数组中创建别名(命名)。它必须在$ routeMiddleware数组中创建。
例如
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,
'checkLogin' => \App\Http\Middleware\CheckLogin::class,
//here you have to add your middleware naming not in the $middleware array
];