Laravel 4 - Get URI of Named Route - laravel-4

Anyone know how to get the uri of a named route? Something like this:
$sourceUri = Route::getNamedRoute('nameOfMyRoute')->getPath(); //this doesn't work of course
The reason I want this is to determine if the current route requested by the client is the same as a given named route like this:
$targetUri = Route::getCurrentRoute()->getPath(); //this DOES work
return sourceUri === targetUri;

This one probably will work:
Route::getRoutes()->getByName('name.of.your.route')->getUri();

Use this:
$url = route('routeName', $params);

Related

Making Routes in Laravel from URL

If our URL is http://127.0.0.1:8000/student/submit-details/1234 then its Route will be:
Route::get('student/submit-details/{id}',
'studentController#submitDetails')->name('submitDetails');
What will be the route if the URL is following?
http://127.0.0.1:8000/student/submit-details?code=1234
I'm using the following Route, but it is not picking it and not working. Does anyone know what will be its Route? I went through the documentation and found no help there.
Route::get('student/submit-details?code={id}', 'MyController#submitDetails');
Your route should be look like as:
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
http://127.0.0.1:8000/student/submit-details?code=1234
in above URL string after the question mark is query parameter and get the value of the query parameter in the controller you should use $_GET:
$_GET['code']
The placeholder parameters for routes are only specified for Route parameters but rather for query parameters. The Route should be only
Route::get('student/submit-details', 'MyController#submitDetails');
You can access the value in the controller via Request instance
public function submitDetails(Request $request) {
dd($request->code);
}
Try this
http://127.0.0.1:8000/student/submit-details?code=1234
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
You have to use get method
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
In Laravel if you want to pass data with GET method :
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
It will give you output like this :
http://127.0.0.1:8000/student/submit-details?code=1234
If you have multiple parameter, it will like :
http://127.0.0.1:8000/student/submit-details?code=1234&code2=5678
You can access the parameter from controller like this :
public function edit(Request $request){
$code = $request->input('code');
dd($code); // 1234
}
Take a look at the $_GET and $_REQUEST superglobals.
If you want route
http://127.0.0.1:8000/student/submit-details?code=1234
Route route will be
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
And usage
route('submitBankDetails', ['code' => 1234])

How to include a variable in a laravel redirect

I have a variable in a Laravel 5 controller which I am trying to include in the url of a redirect link, however nothing I've tried seems to work, my code is currently as follows.
$newChallenge = ((int) request('challenge_id') + 2);
return redirect('/texttext/{{$newChallenge}}');
However, this doesn't use the variable represented by $newChallenge, but rather
"{{newChallenge}}" as a string, how should this be done?
{{ }} is meant for use in blade files.
I think you can do return redirect('/texttext/{ $newChallenge }'); if you really want those { } brackets.
Or just return redirect(sprintf('/texttext/%s'), $newChallenge));
or return redirect('/texttext/'.$newChallenge);
Edit: Also, your redirect could probably benefit from being a named route. https://laravel.com/docs/5.8/routing#named-routes
May be you can change it to a query parameter
like
return redirect('/texttext/?=newChallenge={{$newChallenge}}');
if you want to use
return redirect('/texttext/{{$newChallenge}}');
It should be something like below
Route::get('texttext/{{newChallenge}}', 'YourController#newChallenge');
and newChallenge signature will function newChallenge(Request $request, $newChallenge)
if you want to use something like return redirect(route('new-challenge', $newChallenge));
just add name to route like
Route::get('texttext/{{newChallenge}}', 'YourController#newChallenge')->name('new-challenge');
I have not tested, but I think it should work.

Get part of url from Laravel named route

Is there a possibility to get the part of url, that is defined in route?
For example with this route:
Route::get('/editor/{id}', 'EditorController#editor')->name('editorNew');
after using mentioned functionality, let's say route_link(); i would like to get:
$route_link = route_link('editorNew', array('id' => 1));
//$route_link containts "/editor/1"
I tried to use route(), but i got http://localhost/app/public/editor-new/1 instead of /editor-new/1 and that's not what i wanted.
For clarity need this functionality to generate links depending on machine, that the app is fired on (integration with Shopify).
You can use route method to get the relative path by passing false in the third parameter as:
route('editorNew', [1], false); // returns '/editor-new/1'
You could use the following:
$route_link = route('editorNew', [1]);
1 is the first value that will be on the route, at this moment {id}.
If you want to use the paramater (id) in your method, it will be the following:
public function editor($id) {
//your code
}
And in the view you could use:
Route::input('id');
Hope this works!

how to remove controller name in url in codeigniter

My Current URL Is
http://myaliveidea.com/news/detail/hello/71/In-Ireland-hiking-for-ancient-relics-hidden-by-fog
Then i want to remove controller name
In this URL the controller name is == detail
and function name id = hello
So i want my URL Like this
http://myaliveidea.com/news/hello/71/In-Ireland-hiking-for-ancient-relics-hidden-by-fog
Try this code in routes.php :
$route['hello/(:any)'] = "detail/hello/$1/$2";
or
$route['hello/(:num)/(:any)'] = "detail/hello/$1/$2";
in application/config/routes.php, rewrite the url like this:
$route['news/hello/(:num)/(:any)'] = "news/detail/hello/$1/$2";
I am not sure about the news part, if it is a subfolder in the controller folder than the above line is ok, if CI in installed in news subfolder, then please remove the news part from both side.
Make sure to accept the parameter values from the hello method like this:
public function hello($id = null, $slug = null)
You can change uri path with routes, here there is documentation

Laravel how to route old urls

I am using Laravel 4.
I have an old url that needs to be routable. It doesn't really matter what it's purpose is but it exists within the paypal systems and will be called regularly but cannot be changed (which is ridiculous I know).
I realise that this isn't the format url's are supposed to take in Laravel, but this is the url that will be called and I need to find a way to route it:
http://domain.com/forum/index.php?app=subscriptions&r_f_g=xxx-paypal
(xxx will be different on every request)
I can't figure out how to route this with laravel, i'd like to route it to the method PaypalController#ipbIpn so i've tried something like this:
Route::post('forum/index.php?app=subscriptions&r_f_g={id}-paypal', 'PaypalController#ipbIpn');
But this doesn't work, infact I can't even get this to work:
Route::post('forum/index.php', 'PaypalController#ipbIpn');
But this will:
Route::post('forum/index', 'PaypalController#ipbIpn');
So the question is how can I route the url, as it is at the top of this question, using Laravel?
For completeness I should say that this will always be a post not a get, but that shouldn't really make any difference to the solution.
Use this:
Route::post('forum/{file}', 'PaypalController#ipbIpn');
And then in the controller, use
public function forum($file) {
$request = Route::getRequest();
$q = (array) $request->query; // GET
$parameters = array();
foreach($q as $key => $pararr) {
$parameters = array_merge($parameters, $pararr);
}
}
You can then access the get parameters via e.g.
echo $parameters['app'];
you can use route redirection to mask and ending .php route ex:
Route::get('forum/index', ['uses'=> 'PaypalController#ipbIpn']);
Route::redirect('forum/index.php', 'forum/index');

Resources