I have two routes
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
but when i access the second routes, it always gets the first routes why?
and how to fix it?
thanks for helping me
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->where(['fasilitas_id' => '[0-9]+', 'any' => '[0-9]+']);
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->where('kf_id' => '[0-9]+');
For more info Regular Expression Constraints
Another way to pass it define name route.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('fasilitas.example1');
<a href="{{ route('fasilitas.example1',['fasilitas_id'=>1,'any'=>2]) }}">
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('fasilitas.example2');
<a href="{{ route('fasilitas.example2',['kf_id'=>1]) }}">
When hitting /fasilitas/get_kategori you trigger the first route, with get_kategori being the {fasilitas_id}.
Change the order of your routes, so /fasilitas/get_kategori gets triggered first:
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
The second segment of the first route is a wildcard, that means it could be anything.
When the second route is called in browser the second segment (/get_kategori/) is passing through the wildcard of the first route.
Changing the route order may solve the problem. But the best practice is changing the route name. Example:
Route::get('/fasilitas/something_else/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Try with name . It will be more efficient
Fasilitas !
May be you tried to write facilities.
Whatever, am writing base on ur method.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('detail.fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('get.kategori');
How to call at front page for two parameter and single parater ?
Have a look below .
For detail.fasilitas
Detail Fasilitas
For Single Parameter : get.kategori
Get Kategori
Try it.
Let me know its work or not .
Have fun with code.
Related
I have dozens of routes like the following list
Route::group([
'where' => ['aNumber' => '.*'],
], function () {
Route::get('air/{aNumber}/tools', 'AirController#tools');
Route::post('air/{aNumber}/handled', 'AirController#handled');
Route::post('air/{aNumber}/notHandled', 'AirController#notHandled');
Route::get('air/{aNumber}/act', 'AirController#act');
Route::post('air/{aNumber}/sendAct', 'AirController#sendAct');
Route::get('air/{aNumber}', 'AirController#show');
});
the {anumber} parameter could be like these 23-349493/4 While this kind of parameter will produce some conflicts with other routes, So for example if we are going to call air/28-23422/sendAct then instead of calling #sendAct route, it'll call #show.
because laravel thinks that /sendAct is part of the parameter. Well, I can fix this problem by adding more where not(Regex) on each of the routes and define the logic that every route should follow, But do you have a better solution for this problem?
No, you are wrong, Laravel will choose show since air/28-23422/sendAct won't hit sendAct because sendAct has as supposed method POST, and not GET.
Instead of:
Route::post('air/{aNumber}/sendAct', 'AirController#sendAct');
try writing
Route::get('air/{aNumber}/sendAct', 'AirController#sendAct');
// ^^^
Or use ^[^\/]* as where clause of the group for aNumber
After reading the documentation, I still only have a vague idea of what Named Routes are in Laravel.
Could you help me understand?
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', 'UserProfileController#show')->name('profile');
It says:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function
I don't understand what the second part of the sentence means, about generating URLs or redirects.
What would be a generated URL in the case of profile from the above example? How would I use it?
The best resource is right here : https://laravel.com/docs/5.8/routing#named-routes
One of the common use case is in your views. Say your post request goes to a particular route, basically without named routes you can simply go like this to store a task
action="/task"
but say for example you need to update the route to /task/store , you will need to update it everywhere you use the route.
But consider you used a named route
Route::post('/task', 'TaskController#store')->name('task.store');
With named routes you can use the route like this in your view:
action="{{route('task.store')}}"
Now if you choose to update your route, you only need to make the change in the routes file and update it to whatever you need.
Route::post('/task/now/go/here', 'TaskController#store')->name('task.store');
If you need to pass arguments to your routes, you pass it as arguments to route helper like this:
route('task.edit', 1), // in resource specific example it will output /task/1/edit
All of the view examples are given you use blade templating.
After adding a name to a route, you can use the route() helper to create urls.
This can now be used in your application.
For instance, in your blade templates this may look like:
{{ route('profile') }}
This will use the application url and the route path to create a url.
this is how it looks it:
named route sample name('store');:
Route::get('/store-record','YourController#function')->name('store');
store is the named route here. to call it use route('store')
defining another type of route. this is not named route:
Route::get('/store-record','YourController#function')
you can access this route using {{ url('/store-record') }}
hope this helps
I got this 2 routes in my routes file (web)
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
Route::post('management/special-fees/add', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
They both share the same name but one is GET and the other is POST, so far so good. But now I want to make an url in my view to open the form, for that I use the method route() like this
route('management/special-fees/add',$user->id )
but when trying to go to the url I get this route
.../management/special-fees/add?5
there is a question mark instead of a "/" so the route is invalid.
I made some tests and I figured out that happens because is trying to go to the POST route instead of the GET one if I change the POST route's url in the web file like this
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
I will in fact get this url
.../management/special-fees/addSSSS?5
So why is the route() method generating a url for the POST route over the GET one? how do I make it to choose the GET route first?
In laravel the routing is prioritized by in the order it is written in your route.php file.
In this case you're writing the Route::post last, which in turn tells Laravel that that one should have the highest priority. Try switching them and the Route::get will have the higher priority.
Like so:
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
I may be wrong, but I think you'll have to re-think route naming. One of the problems route naming helps eliminate is redundant and complex names. For example, if you looked at route:list for Route::resource('something', 'SomethingController') it will have something.index, something.store as route names for Route::get('something') and Route::post('something').
If it's the same name, it will always resolve to the first one and will probably never hit the second route; in your case will hit the POST route and never the GET route.
?5 means 5 is an argument for your get route.
try this
url('management/special-fees/add/'.$user->id)
for get route insted of
route('management/special-fees/add',$user->id )
I'm trying to create a route with an array of aliases, so when I call whois or who_is in the url it goes to the same route.
Then I don't need to keep repeating the code every time, changing only the alias.
I tried the code below.
Variables in the routes:
$path = 'App\Modules\Content\Controllers\ContentController#';
$aliases['whois'] = '(quemsomos|who_is|whois)';
Routes:
Route::get('{whois}', array('as' =>'whois', 'uses' => $path.'getWhois'))->where('whois', $aliases['whois']);
this one works as well
Route::get('{whois}', $path.'getWhois')->where('whois', $aliases['whois']);
Typing in the url my_laravel.com/whois or my_laravel.com/who_is or my_laravel.com/quemsomos will send me to $path.'getWhois' (which is correct).
But when I try to call it in the html on blade...
Who we are
The reference link goes to my_laravel.com//%7Bwhois%7D
How could I call route('whois') on my blade.php and make it work like when I type it on the url ?
I would like to use the route function` in my blade, so I can keep a pattern.
During route generation using the route function, Laravel expects you to set the value of the route parameter. You are leaving the parameter whois empty so the parameter capturing {whois} will not be replaced and results in the %7B and &7D for the accolades.
So in order to generate a route you will need to define what value you'd like to use for whois; {{ route('whois', ['whois'=>'whois']) }} for instance.
what is the difference between both routing ? can anyone explain ?
Route::get('login', 'webcontroller#login');
Route::get('login', array('as' => 'login','uses'=>'webcontroller#login'));
Well. There is the flexibility of Route object (i think it belongs to Symfony)
In the first statement, you explicitly say that you what controller's action a certain address should trigger (in your case it is 'login' which triggers login() of WebController).
In the second statement, you can add an "array" of settings for the controller's method, which, in your case you have specified a name. "login", which is the name of your Route::get() rule for the address "/login", could be used any where in the system without you explicitly specifying any controller or url which gives you the ability to change whatever you like in the future, as long as you are consistent with your names.
You set a route:
Route::get("login", array('as'=>'login', 'uses'=>'LoginController#Login');
Then you can use it like:
$url = URL::route('profile');
Whil you are still able to change the url of your route:
Route::get("user/login", ...);
Without the need to change its uses of "name" within your project.
You can read about it on Laravel's official documentation:
http://laravel.com/docs/4.2/routing#named-routes
In the number 2, you usea an alias , is easy for call de route in the code:
example:
<a href=" {{ route('user.list') }} ">
< span class="glyphicons glyphicons-link"></span>
<span class="sidebar-title">Link</span>
</a>