Tôi đang đặt cookie trên một số sự kiện nhấp chuột. Sau đó, sau khi lưu trữ giá trị trong cookie, tôi muốnCách làm việc với các cookie trong Laravel 5.2
- Kiểm tra sự tồn tại của Cookie
- giá trị cookie Nhận
tôi đã phát triển một chức năng bằng cách tham khảo Laravel tài liệu chính thức. Bảng điều khiển cho thấy rằng cookie đã được đặt. Nhưng sau đó, tôi không thể giải quyết hai điểm (được đề cập trong danh sách trên) cho xem (Blade Template). Nó luôn hiển thị (Cookie::get('cookie.clients'))
'null'. Nhưng bảng điều khiển trình duyệt hiển thị cookie đó . Nếu có ai biết câu trả lời, nó sẽ được đánh giá cao.
Đây là mã của tôi.
khiển
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');
}
}
Xem
@if (Cookie::get('cookie.clients') != null)
<p>cookie is set</p>
@else
<p>cookie isn't set</p>
@endif
Bạn có đặt cookie qua javascript trong "một số sự kiện nhấp chuột" hay bằng cách gọi phương thức 'cookieadd' trong' Clientcontroller' của bạn? – Padarom
@Padarom, bằng cách gọi phương thức 'cookieadd()' –