I am having an issue trying to make a GET request to a route and process the parameters passed in via the URL. Here are two routes I created in routes.php:
$router->get('/', function() {
$test = \Input::get('id');
dd($test);
});
$router->get('test', function() {
$test = \Input::get('id');
dd($test);
});
When I go to the first URL/route ('/') and pass in some data, 123 prints to the screen:
http://domain.com/dev/?id=123
When I go to the second ('test') NULL prints to the screen (I've tried '/test' in the routes.php file as well).
http://domain.com/dev/test?id=123
A few things to note here:
This installation of Laravel is in a subfolder.
We are using Laravel 5.
Any idea why one would work and the other would not?
First thing - Laravel 5 is still under active development so you cannot rely on it too much at this moment.
Second thing is caching and routes. Are you sure you are running code from this routes?
Change this code into:
$router->get('/', function() {
$test = \Input::get('id');
var_dump($test);
echo "/ route";
});
$router->get('test', function() {
$test = \Input::get('id');
var_dump($test);
echo "test route";
});
to make sure messages also appear. This is because if you have annotations with the same verbs and urls they will be used and not the routes you showed here and you may dump something else. I've checked it in fresh Laravel 5 install and for me it works fine. In both cases I have id value displayed
You can use
Request::path()
to retrieve the Request URI or you can use
Request::url()
to get the current Request URL.You can check the details from Laravel 5 Documentation in here : http://laravel.com/docs/5.0/requests#other-request-information and when you did these process you can get the GET parameters and use with this function :
function getRequestGET($url){
$parts = parse_url($url);
parse_str($parts['query'], $query);
return $query;
}
For this function thanks to #ruel with this answer : https://stackoverflow.com/a/11480852/2246294
Try this:
Route::get('/{urlParameter}', function($urlParameter)
{
echo $urlParameter;
});
Go to the URL/route ('/ArtisanBay'):
Hope this is helpful.
Related
I recently updated a project from Laravel 5.4, to 9.X using Laravel shift.
I had a language controller that would allow me to swap language, and it would also translate my URLs.
It seems like this doesn't work anymore:
$route = app('router')->getRoutes()->match(app('request')->create($previous_request->getUri()));
Actually, I did something like this before, but neither work
$route_name = app('router')->getRoutes()->match($previous_request)->getName();
By not work, I mean, if I debug like this:
Log::debug("URL: " . $previous_request->getUri());
$route = app('router')->getRoutes()->match(app('request')->create($previous_request->getUri()));
Log::debug('Route: ' . $route);
laravel.log will containt URL: and the correct url, but Route: never shows up.
Did I miss some breaking change going from 5.4 to 9.X? How would I now get the route from an url?
My Current URL: http://127.0.0.1:8000/users/list
$currentRouteName = Route::currentRouteName();
dd($currentRouteName);
Output: users.list
$currentRouteAction = Route::currentRouteAction();
dd($currentRouteAction);
Output: App\Http\Controllers\UserController#userLists
I'm trying to create a function album_create() that handles both insert and update process according to the parameters sent. this function needs routing to make the url prettier, so this is my routes.php:
$route['community/(\w.+)/album/create/(\w.+)'] = 'community/album_create/$1/$2';
$route['community/(\w.+)/album/create'] = 'community/album_create/$1';
$route['community/(\w.+)/album'] = 'community/album/$1';
and this is the php controller methods :
function album() {
// some code here
}
function album_create($parent_id, $post_id='') {
// some code here
}
unfortunately, only the second and third line of routing works, but the first line doesn't.
/community/12/album/create/9 // this url doesn't work
/community/12/album/create // this url works
/community/12/album // this url works
by doesn't work, I mean it only shows 404 Page Not Found
does anyone have an idea where the problem is??
First off, apologies if this is a bad question/practice. I'm very new to Laravel, so I'm still getting to grips with it.
I'm attempting to pass a variable that contains forward slashes (/) and backwards slashes () in a Laravel 5 route and I'm having some difficulties.
I'm using the following: api.dev/api/v1/service/DfDte\/uM5fy582WtmkFLJg==.
Attempt 1:
My first attempt used the following code and naturally resulted in a 404.
Route:
Route::group(array('prefix' => 'api/v1'), function() {
Route::resource('service', 'ServiceController');
});
Controller:
public function show($service) {
return $service;
}
Result:
404
Attempt 2:
I did a bit of searching on StackOverflow and ended up using the following code, which almost works, however, it appears to be converting \ to /.
Route:
Route::group(array('prefix' => 'api/v1'), function() {
Route::get('service/{slashData}', 'ServiceController#getData')
->where('slashData', '(.*)');
});
Controller:
public function getData($slashData = null) {
if($slashData) {
return $slashData;
}
}
Result:
DfDte//uM5fy582WtmkFLJg==
As you can see, it's passing the var but appears to be converting the \ to /.
I'm attempting to create an API and unfortunately the variable I'm passing is out of my control (e.g. I can't simply not use \ or /).
Does anyone have any advice or could point me in the right direction?
Thanks.
As you can see from the comments on the original question, the variable I was trying to pass in the URL was the result of a prior API call which I was using json_encode on. json_encode will automatically try and escape forward slashes, hence the additional \ being added to the variable. I added a JSON_UNESCAPED_SLASHES flag to the original call and voila, everything is working as expected.
You should not do that. Laravel will think it is a new parameter, because of the "/". Instead, use htmlentitites and html_entity_decode in your parameter, or use POST.
I use Symfony 2.3.x for an application I currently develop.
I have this structure in my app/config/ folder:
environment/dev/file.yml
environment/test/file.yml
environment/myenv/file.yml
and so on
In each of these files, I have something like:
parameters:
myvalue: 2000
In my Controller, I do this:
$v = $this->container->getParameter('myvalue');
var_dump($v);
The above code will dump the value int 2000.
So everything works great.
But I want to handle the situation where myvalue is not defined. If I simply delete this variable from the YAML files, I get an HTTP 500 error with a huge stack-trace.
I want to do something like:
if (variable_exists ('myvalue')) {
$x = $this->container->getParameter('myvalue');
else {
$x = SOME_DEFAULT_VALUE;
}
useVariable($x);
Does Symfony have some sort of function that does what the variable_exists() function mentioned above does ?
I couldn't find anything in the docs.
Thank you in advance.
You could try calling $container->hasParameter('myvalue');. See here for more details.
I try to set a Cookie in Laravel 4 in a specific route.
Unfortunately, setting the Cookie does only work in the global App::after() filter.
First thing I tried was returning a response with a Cookiefrom my Controller.
This doesn't work:
return Response::make($view)->withCookie(Cookie::make('foo','bar'));
However, this does:
return Response::make()->withCookie(Cookie::make('foo','bar'));
But does not solve my problem.
Next I tried it with an after filter as follows.
Route::filter('cookie', function($route, $request, $response)
{
$response->withCookie(Cookie::make('foo', 'bar'));
});
This does not work either.
Next, I tried it using Cookie::queue(), which I've found in another answer - with no luck.
The only place the Cookie is set properly is in App::after().
App::after(function($request, $response)
{
$response->withCookie(Cookie::make('foo', 'bar'));
});
Besides I'm pretty sure that one of the other approaches should work, this solution doesn't give me the control I'm looking for.
I'm running Laravel v4.0.9.
Try this tested, working code.
Specify expiration time (in minutes from now). Dont you use some cookie extension in your browser, which may protect/blacklist specified cookies from being modified...
Route::get('cookieset', function(){
$cookie = Cookie::make('foo', 'bar', 60);
return Redirect::to('cookieget')->withCookie($cookie);
});
Route::get('cookieget', function(){
dd(Cookie::get('foo'));
});