How to get cookie in octobercms?

Member

by samara , in category: PHP CMS , 3 months ago

How to get cookie in octobercms?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 3 months ago

@samara 

To get a cookie in OctoberCMS, you can use the Request class provided by the framework. Here's an example of how to retrieve a cookie value:


First, inject the Request class into your controller or component:

1
2
3
4
5
6
7
8
9
use IlluminateHttpRequest; // Import the Request class

class SomeController extends Controller
{
    public function index(Request $request)
    {
        // Your code here
    }
}


Then, you can use the cookie() method on the $request object to retrieve a specific cookie value:

1
2
3
4
5
6
public function index(Request $request)
{
    $cookieValue = $request->cookie('cookie_name'); // Replace 'cookie_name' with the actual name of the cookie

    // Your code here
}


Make sure to replace 'cookie_name' with the actual name of the cookie you want to retrieve.


You can also use the hasCookie() method to check if a cookie exists before retrieving its value:

1
2
3
4
5
6
public function index(Request $request)
{
    $hasCookie = $request->hasCookie('cookie_name'); // Replace 'cookie_name' with the actual name of the cookie

    // Your code here
}


These methods allow you to access cookies sent with the request in OctoberCMS.