I'm totally new to Laravel framework just started learning few days back, and I challenged my self to create cookie based URL. Meaning, if I were to access http://localhost/cookie/nick , it should store a cookie based on cookies/*. I tried to do but I get empty cookie each time. Is it possible to do it? If yes can anyone kindly guide me? Here is my code:-
Routes.php
Route::get('/cookie/{name}','CookieControllerName@setCookieByUrl');
CookieControllerName.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
class CookieControllerName extends Controller
{
public function getCookie(Request $request){
$value = $request->cookie('name');
echo "Hello ".$value;
}
public function setCookieByUrl(Request $request, $name){
$response = new Response('Hello World');
$response->withCookie(cookie('name', $name, 3600));
$value = $request->cookie('name');
echo $value; //For debugging purpose
// getCookie();
}
}
You need to return the $response variable for anything to happen.
Besides that I don't really see how your code could be working since you have a function call to getCookie() that is not even in the scope. And it should be:
$this->getCookie()
Simply replace getCookie() with:
return $response
Should suffice in atleast getting a proper cookie set.
Complete codeblock:
public function setCookieByUrl(Request $request, $name){
$response = new Response('Cookie has been set');
$response->withCookie(cookie('name', $name, 3600));
return $response;
}
Cookie value is encrypted that is why you won't see it directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.