Я разрабатываю API с использованием laravel, поэтому определенно не хочу использовать файлы cookie. Однако я хочу использовать механизм сеансов для API, требующих аутентификации.
Итак, я использую sessions.driver = "file"
Чтобы иметь возможность использовать этот механизм, но разрешить переопределять набор файлов cookie, после долгих отладок я обнаружил, что в классе промежуточного программного обеспечения есть некоторые аппаратные средства, но с помощью магии фильтров вы можете отключить эту функцию прямо перед тем, как файл cookie набор.
Итак, filters.php
я создал следующий фильтр и добавил в качестве фильтра after
моей группы маршрутов
/*
|--------------------------------------------------------------------------
| Custom Filter to remove the session cookie
|--------------------------------------------------------------------------
|
| By default, if session driver is other than `null` or `array`, it will
| create a cookie and pass the encrypted session id so that it can be used
| across web requests.
| However, since our application is an API, we dont need the cookie, but
| we still want to be able to use the session functionality, so to allow
| this, we just need to set the driver to `array` right before the
| dispatcher gets to the point to add the session cookie.
|
| This is the Laravel call stack
| \Illuminate\Session\Middleware::handle()
| -> \Illuminate\Session\Middleware::addCookieToResponse()
| -> \Illuminate\Session\Middleware::sessionIsPersistent()
|
| All session handling and file storage has happened before sessionIsPersistent()
| is called, so we are safe to add an `after` filter that will reset
| the driver in the configuration and thus preventing this specific
| cookie to be added, all other previously added cookies will be
| kept (if any added) and thus sent as part of the response.
*/
Route::filter('session.cookie.remove', function(){
// Has to be 'array' because null, will prevent from writing sessions
Config::set('session.driver', 'array');
});
Примечание. единственный случай, когда этот фильтр не будет вызываться и, таким образом, создавать файл cookie, — это возникновение исключения. В этом случае вы также можете обновить конфигурацию своего обработчика ошибок (ошибка по умолчанию обработчик, если вы не перезаписали laravel). Чтобы переопределить, посмотрите на app/start/global.php
person
AlphaZygma
schedule
13.10.2014
auth.basic
? - person Muhammad Usman   schedule 07.05.2013WWW-Authenticate
, чего я не хотел. Он также устанавливаетlaravel_session
cookie. - person Iso   schedule 07.05.2013