为lumen添加session支持,同时配置全局函数csrf_token可用
首先laravel和lumen框架的版本要一致,我这里版本都是5.4
1.复制laravel框架config目录下的session.php到lumen框架config目录下
2.修改bootstrap目录下的app.php
添加
//加载配置
$app->configure('session');
//注册中间件
$app->middleware([
IlluminateSessionMiddlewareStartSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
]);
//注册服务提供者
$app->register(IlluminateSessionSessionServiceProvider::class);
//添加全局函数csrf_token
3.修改vendorlaravellumen-frameworksrchelpers.php
添加(该函数从拷贝自laravel框架下vendorlaravelframeworksrcIlluminateFoundationhelpers.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.添加文件appHttpMiddlewareVerifyCsrfToken.php拷贝自laravel
appHttpMiddlewareVerifyCsrfToken.php
5.添加文件vendorilluminatehttpMiddlewareVerifyCsrfToken.php拷贝自
laravel框架vendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php
该文件拷贝过去后需要修改命名空间,因为lumen下命名空间变化了
6.修改vendorlaravellumen-frameworksrcApplication.php
在方法
registerContainerAliases中添加
'IlluminateSessionSessionManager'=>'session',别名