【发布时间】:2016-03-22 09:01:31
【问题描述】:
我在一些点击事件上设置 cookie。然后在cookie中存储值之后,我想
- 检查 cookie 是否存在
- 获取 cookie 值
我通过参考 Laravel 官方文档开发了一个功能。控制台显示 cookie 已设置。但在那之后,我无法解决视图(刀片模板)的两点(在上面的列表中提到)。它总是显示(Cookie::get('cookie.clients'))'null'。但浏览器控制台显示该 cookie
.
如果有人知道答案,将不胜感激。
这是我的代码。
控制器
use App\Client;
use App\Http\Requests;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Repositories\ClientRepository;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
class ClientController extends Controller
{
public function cookieadd(Request $request, Client $client)
{
$clients = [];
if (Cookie::get('cookie.clients') != null)
{
$clients = Cookie::get('cookie.clients');
}
array_push($clients, $client);
Cookie::forever('cookie.clients', $clients);
return redirect('/client');
}
}
查看
@if (Cookie::get('cookie.clients') != null)
<p>cookie is set</p>
@else
<p>cookie isn't set</p>
@endif
【问题讨论】:
-
你是在“一些点击事件”中通过javascript设置cookie,还是在你的
Clientcontroller中调用cookieadd方法? -
@Padarom,通过调用方法
cookieadd()
标签: php laravel laravel-5.2