Laravel饼干在serviceprovider不可比 [英] Laravel cookie in serviceprovider not comparable

查看:129
本文介绍了Laravel饼干在serviceprovider不可比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试传递一个变量,基于我的compose函数中的cookie值来创建我的菜单,使用serviceproviders 在此推荐

I try to pass a variable based on a cookie value in my compose function to all my view to build my menu, with the use of serviceproviders recommmended here:


:Providers / ViewComposerServiceProvicer.php

File: Providers/ViewComposerServiceProvicer.php



public function boot(Request $request) { $this->composeTopBar($request);}

public function composeTopBar(Request $request)
{
     $cookieValue = $request->cookie('brand');
    // if value not set use default value.
    if($cookieValue == null)
    {
        $cookieValue = 1;
    }
    $brands = \App\Brand::orderBy('priority', 'asc')->get();
    foreach($brands as $brand){
        if($brand->id == $cookieValue){
            $brand->menuActive = true;
        }
        else{
            // show value to debug
            $brand->menuActive = $cookieValue;
        }
    }


    view()->composer('front.layouts.top', function ($view) use ($brands) {
        $view->with('brandItems',$brands );
    });
}

cookieValue看起来像

the cookieValue looks like

yJpdiI6IlNJODBvQ1RNM004OWVleyJpdiI6IlNJODBvQ1RNM004OWVleyJpdiI6IlNJODBvQ1RNM004OWVl

' 2 '如何在我的撰写功能中获得原始值 2

While the value in my controller looks like '2' How can i get the original value 2 in my compose function?

我需要得到原始值来比较它在我的composeTopBar函数,所以我可以传递一个变量为true如果它等于cookie值。

I need to get the original value to compare it in my composeTopBar function so I can pass a variable to be true if it equals the cookie value.

设置cookie的方法

Method to set cookie

    $response = response()-> view('front.products.category', compact('products','category'));
    $response->withCookie(cookie()->forever('brand',1));
    return $response;


推荐答案

我最终使用了 class based composer
这个原因是因为它在laravel的生命周期中被调用,并且Cookie变量被解密。当使用基于Closure的composers时,这些值是加密的。

I ended up using a class based composer . The reason why this works is because it's called later in the lifecycle of laravel and the Cookie variables are decrypted. When using Closure based composers the values are encrypted.

这篇关于Laravel饼干在serviceprovider不可比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆