php - Cookie 在 Laravel 中不起作用?

标签 php laravel cookies

当新用户访问我的网站时,我想将他们重定向到/welcome 但只有一次,我想这样做我可以使用 cookie 并在他们访问欢迎页面后永久设置 cookie,然后检查 cookie 是否存在在将它们发送到/welcome 之前。

这里我有一个基本 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();
        }
    }
}

当发送到 frontend.guest.welcome 时,它有一个到 WelcomeController 的路由

Route::get('/welcome', ['uses' => 'WelcomeController@getView', 'as' => 'frontend.guest.welcome']);

这是WelcomeController

class WelcomeController extends BaseController
{
    public function getView()
    {
        Cookie::forever('visited_welcome', '1');

        return view('frontend.guest.welcome');
    }
}

问题是,它不断地发送到/welcome,不是一次而是总是。

最佳答案

尝试在 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'
    ];
}

关于php - Cookie 在 Laravel 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48491918/

相关文章:

php - 无法让 PHP 从 mySQL 中提取行条目作为数组

php - laravel eloquent ->whereHas - 编写你自己的存在(子查询)

Laravel 5 模型继承 - 获取子模式时返回父数据

jQuery - 基于访问次数的 CSS 样式切换器,使用 cookie

javascript - 单击按钮时使复选框 cookie 过期

java - 使用 JSTL 验证 JSP 中的 Cookie

php - 如何使用PHP + mySQL创建一系列DIV?

php - 如何检查 PHP 数组是否设置了任何值?

php - 如何将MySQL表中的日常记录存储到另一个表中?

php - 解析 CSS url 时出现 Webpack 错误