Laravel - Get route by a route name - laravel

route('products.create') returns full path like http://myapp.dev/products/create.
How do I get the actual route? Only this -> products/create?

There is no such functionality provided by Laravel at this point. However, you may try this:
$extra = URL::to('/');
$actual = route('products.create');
str_replace($extra, '', $actual);
That will remove the unnecessary base URL from your route URL.

Related

What to do when laravel routing is duplicated in laravel

I have added two routes, as described below.
I want the following behavior.
/api/user/1 or /api/user/2 or . . . → UserController#show is excute
/api/user/auth → UserController#showAuthUser is excute
However, when call /api/user/auth, UserController#show is excuted.
How do I get what I want?
Route::get('user/{user_id}', 'Api\UserController#show');
Route::get('user/auth', 'Api\UserController#showAuthUser');
Put your routes with fixed "parameters" first :
Route::get('user/auth', 'Api\UserController#showAuthUser');
Route::get('user/{user_id}', 'Api\UserController#show');
If you put {user_id} first, Laravel will put "auth" in your "user_id" variable
Switch the order of your routes
Route::get('user/auth', 'Api\UserController#showAuthUser');
Route::get('user/{user_id}', 'Api\UserController#show');
Otherwise it expects 'auth' to be a 'user_id'

Laravel get Route parameters outside controller

I have defined a dummy route like this:
Route::get('sth/{v1}/{v2}' , [
'uses'=>'SthController#sth',
]) ;
how can I get the value of v1 and v2, outside controllers?
use this code
$current_params = Route::current()->parameters();
dd($current_params->v1) ;
You can get the values of v1 and v2 anywhere like this:
request()->v1;
request()->v2;
In Laravel 5.6 for me it was:
Route::current()->parameters['v1']
Route::current()->parameters['v2']
etc...
You can put the data in session in controller when pass, then from anywhere you can get your desire data,
Session::put('v1');
Session::put('v2');
now anywhere you can access like:
Session::get('v1')
Session::get('v2')
if you need to delete session data just use
Session::forget('v1')
Session::forget('v2')
You can use the laravel helper named: request()
$value = request('key', $default);
For your route you can use
$v1 = request('v1','default data');
$v2 = request('v2','default data');
Laravel documentation: request helper laravel
haven't tried but think its Route::current(), use from anywhere to access the parameters
$currentParams = Route::current()->parameters();
This can be alternative way:
Route::getCurrentRoute()->getParameter('v1')

laravel paginate passing additional var

let say i have url like this
http://localhost:8080/myweb/listproduct?idcategory=3
but when i used laravel pagination link, my passing variabel by url disappear like this
http://localhost:8080/myweb/listproduct?page=2
i want laravel paginate to pass my url variable too like this
http://localhost:8080/myweb/listproduct?idcategory=3&page2
so far i already try to set baseurl like this
$listproduct = DB::table('product')->paginate(15);
$users->setBaseUrl('listproduct?idcategory=3');
but the result is like this (? not &)
http://localhost:8080/myweb/listproduct?idcategory=3?page2
i used laravel 4.2
you can use setPath()
$users->setPath('listproduct?idcategory=3');
or if you only want to add to the query string you may use
$users->appends(['idcategory' => '3'])
You can use this method to append more arguments to your pagination link:
$listproduct->appends(['idcategory' => $id])->links();

Routing in Codeigniter (:any) gives me strange results

Here i'm using below routing config
$route['controllername/(:any)'] = "controllername/index/$1";
this is working well for me, but i want to use other methods in the same controller.
for that i'm using below route
$route['controllername/search'] = "controllername/search";
this is also working fine, but i want pass parameters to this method.
Here if i'm passing parameters but it is calling index method
i want to use both of above routes, i have tried with below route also but same result
$route['controllername/search/(:any)'] = "controllername/search/$1";
could anyone give any suggestions?
Thankyou!!
Ok.Do it like this for routing multiple functions in same controller :
$route['default_controller'] = "controller_name";
$route['index/(:any)'] = "controller_name/index/$1";
$route['search/(:any)'] = "controller_name/search/$1";
Let me know if you find any issues or doubts.
#user4039421
change your roouting rules position like below
$route['controllername/search/(:any)'] = "controllername/search/$1";
$route['controllername/search'] = "controllername/search";
$route['controllername/(:any)'] = "controllername/index/$1";

Coginter Clean Url

I am using Coginter
i want to clean url
i have url http://androidsign.com/newarticle//categories/get_categories/electronics
i want this url http://androidsign.com/newarticle/electronics
in routes file i write code
$route['categories/(:any)'] = "$1";
But this is not working
Can anybody have idea
Thanks
Here it is,
current : http://androidsign.com/newarticle//categories/get_categories/electronics
desired : http://androidsign.com/newarticle/electronics
then on application/config/routes.php
add this routing RULE
//if class = newarticle , method = categories , 3rd seg = get_categories ,4th (:any) wildcard
$route['newarticle/categories/get_categories/:any'] = "newarticle/electronics";
A URL containing the segments newaricle/categories/get_categories will be remapped to the class newarticle and method electronics

Resources