ʞɹᴉ*_*ʌɐp 11 php session laravel laravel-4 laravel-routing
我正在使用laravel和默认会话驱动程序设置为REDIS为我的Android应用程序构建API.
我在http://dor.ky/laravel-prevent-sessions-for-routes-via-a-filter/找到了一篇很好的文章.
然而,当我点击URL时,它也会击中redis并生成空的键.现在我想避免在redis中创建空的会话密钥.理想情况下它不应该击中redis我该怎么做?
我们能否以某种方式自定义sessios,以便仅为特定路由生成会话(或禁用特定路由)?
我可以用具体的用例解释一下,请告诉我.
使用Laravel 5中的中间件非常容易,我需要一个没有会话的API密钥请求我只是做了:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\Middleware\StartSession as BaseStartSession;
class StartSession extends BaseStartSession
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(\Request::has('api_key'))
{
\Config::set('session.driver', 'array');
}
return parent::handle($request, $next);
}
}
Run Code Online (Sandbox Code Playgroud)
您还需要按如下方式扩展SessionServiceProvider:
<?php namespace App\Providers;
use Illuminate\Session\SessionServiceProvider as BaseSessionServiceProvider;
class SessionServiceProvider extends BaseSessionServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerSessionManager();
$this->registerSessionDriver();
$this->app->singleton('App\Http\Middleware\StartSession');
}
}
Run Code Online (Sandbox Code Playgroud)
并放在你的config/app.php下providers:
'App\Providers\SessionServiceProvider',
Run Code Online (Sandbox Code Playgroud)
您还必须在内核文件中更改它:App/Http/Kernel.php在$middlewareGroups更改默认条目的部分中,更改为\Illuminate\Session\Middleware\StartSession::class,新类\App\Http\Middleware\StartSession::class,.
在Laravel 5,只是不使用StartSession,ShareErrorsFromSession和VerifyCsrfToken中间件。
在我的应用程序中,我已将这三个中间件从web组移动到一个新stateful组,然后我将此stateful组包含在需要了解会话的路由中(除了web在所有情况下,至少在我的应用程序中)。其他路由属于web或api组。
现在,当向不使用stateful中间件组会话 cookie的路由发出请求时,不会发回。
似乎有一种方法可以使用会话拒绝回调来完成此操作。
相关来源...
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/Application.php#L655
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/Application.php#L660
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Session/Middleware.php#L60
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Session/Middleware.php#L97
我在网络上找不到很多对此的引用,但是通过源代码阅读更多内容似乎如果会话拒绝回调返回真值,则会话将被迫使用数组驱动程序来处理请求,而不是配置任何内容。您的回调还会获取注入的当前请求,以便您可以根据请求参数执行一些逻辑。
我只在本地 Laravel 4.2 安装上对此进行了测试,但它似乎有效。您只需要将一个函数绑定到 session.reject 即可。
首先,创建一个 SessionRejectServiceProvider (或类似的东西)
<?php
use \Illuminate\Support\ServiceProvider;
class SessionRejectServiceProvider extends ServiceProvider {
public function register()
{
$me = $this;
$this->app->bind('session.reject', function($app)use($me){
return function($request)use($me){
return call_user_func_array(array($me, 'reject'), array($request));
};
});
}
// Put the guts of whatever you want to do in here, in this case I've
// disabled sessions for every request that is an Ajax request, you
// could do something else like check the path against a list and
// selectively return true if there's a match.
protected function reject($request)
{
return $request->ajax();
}
}
Run Code Online (Sandbox Code Playgroud)
然后将其添加到 app/config/app.php 中的提供程序中
<?php
return array(
// ... other stuff
'providers' => array(
// ... existing stuff...
'SessionRejectServiceProvider',
),
);
Run Code Online (Sandbox Code Playgroud)
最终结果是,在会话启动之前,对应用程序的每个请求都会调用reject() 方法。如果您的reject()方法返回true,会话将被设置为数组驱动程序并且基本上不执行任何操作。你可以在 $request 参数中找到很多有用的信息来确定这一点,这里是 4.2 中 request 对象的 API 参考。
http://laravel.com/api/4.2/Illuminate/Http/Request.html
| 归档时间: |
|
| 查看次数: |
7012 次 |
| 最近记录: |