爲lumen添加session支持,同時配置全局函數csrf_token可用php
首先laravel和lumen框架的版本要一致,我這裏版本都是5.4laravel
1.複製laravel框架config目錄下的session.php到lumen框架config目錄下bootstrap
2.修改bootstrap目錄下的app.phpsession
添加app
//加載配置框架
$app->configure('session');ide
//註冊中間件函數
$app->middleware([
Illuminate\Session\Middleware\StartSession::class,
App\Http\Middleware\VerifyCsrfToken::class,
]);spa
//註冊服務提供者csrf
$app->register(Illuminate\Session\SessionServiceProvider::class);
//添加全局函數csrf_token
3.修改\vendor\laravel\lumen-framework\src\helpers.php
添加(該函數從拷貝自laravel框架下\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php)
if (! function_exists('csrf_token')) {
/**
* Get the CSRF token value.
*
* @return string
*
* @throws \RuntimeException
*/
function csrf_token()
{
$session = app('session');
if (isset($session)) {
return $session->token();
}
throw new RuntimeException('Application session store not set.');
}
}
4.添加文件\app\Http\Middleware\VerifyCsrfToken.php拷貝自laravel
\app\Http\Middleware\VerifyCsrfToken.php
5.添加文件\vendor\illuminate\http\Middleware\VerifyCsrfToken.php拷貝自
laravel框架\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php
該文件拷貝過去後須要修改命名空間,由於lumen下命名空間變化了
6.修改\vendor\laravel\lumen-framework\src\Application.php
在方法
registerContainerAliases中添加
'Illuminate\Session\SessionManager'=>'session',別名