Cookie 在 Laravel 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48491918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cookies not working in Laravel?
提问by Adam G
When a new user visits my site I want to redirect them to /welcome but only once, I thought to do this I could use cookies and set a cookie forever once they had visited the welcome page and then checking if the cookie existed before sending them to /welcome.
当新用户访问我的网站时,我想将他们重定向到 /welcome 但只有一次,我想这样做我可以使用 cookie 并在他们访问欢迎页面后永远设置 cookie,然后在发送之前检查 cookie 是否存在欢迎。
Here I have a base controller
这里我有一个基本控制器
class BaseController extends Controller
{
public function __construct(Request $request)
{
$this->checkWelcome($request);
}
private function checkWelcome(Request $request) {
$currentRoute = Route::currentRouteName();
if ($currentRoute != 'frontend.guest.welcome' && Cookie::get('visited_welcome') != '1') {
header('location: ' . route('frontend.guest.welcome'));
exit();
}
}
}
When sending to frontend.guest.welcome
it has a route to WelcomeController
当发送到frontend.guest.welcome
它有一个到 WelcomeController 的路由
Route::get('/welcome', ['uses' => 'WelcomeController@getView', 'as' => 'frontend.guest.welcome']);
Here is WelcomeController
这是 WelcomeController
class WelcomeController extends BaseController
{
public function getView()
{
Cookie::forever('visited_welcome', '1');
return view('frontend.guest.welcome');
}
}
The issue is, its constantly sending to /welcome, not once but always.
问题是,它不断地发送到 /welcome,不是一次而是总是。
回答by Adam G
You aren't returning the cookie with the response, attach it to the response like so:
您没有将 cookie 与响应一起返回,请将其附加到响应中,如下所示:
public function checkWelcome(Request $request) {
{
if (!$request->cookie('visited_welcome')) {
return redirect('frontend.guest.welcome')->withCookie(Cookie::forever('visited_welcome', '1'));
}
// otherwise proceed as normal
}
Alternatively, you can use the queue
method on the Cookie facade:
或者,您可以使用queue
Cookie 外观上的方法:
Cookie::queue(Cookie::forever('visited_welcome', '1'));
https://laravel.com/docs/5.5/responses#attaching-cookies-to-responses
https://laravel.com/docs/5.5/responses#attaching-cookies-to-responses
A better approach may be using middleware, that way you wouldn't need to implement any check in your controller code. For example:
更好的方法可能是使用中间件,这样您就不需要在控制器代码中实现任何检查。例如:
// CheckIfFirstTimeVisit.php
public function handle(Request $request, Closure $next)
{
if ($request->cookies->has('visited_welcome')) {
return $request($next);
}
return response()->view('frontend.guest.welcome')
->withCookie(Cookie::forever('visited_welcome', '1'));
}
回答by ExeCiety
try to except your cookie in app\Http\Middleware\EncryptCookies.php
尝试在 app\Http\Middleware\EncryptCookies.php 中排除您的 cookie
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'visited_welcome'
];
}