I have a little problem ( Laravel 5.0 ) with routing.
When I put this in routes.php
Route::get('admin/tags', 'Admin\TagController#index');
Route::get('admin/tags/{id}','Admin\TagController#show');
Route::get('admin/tags/create', 'Admin\TagController#create');
last route admin/tags/create show blank page.
but when i change order to ( create 1st )
Route::get('admin/tags/create', 'Admin\TagController#create');
Route::get('admin/tags', 'Admin\TagController#index');
Route::get('admin/tags/{id}','Admin\TagController#show');
everything is ok and admin/tags/create view showing content.
when the users asks for
/admin/tags/create
the requested url will also match
Route::get('admin/tags/{id}','Admin\TagController#show');
because it can pars your "create" word as the {id} part of the route definition.
to solve this, you can use some reqular expressions that describe your {id} as an string, consists of numbers 0-9 .
Related
The question might sound simple but what's the difference between those 2 routes exactly ?
why dont we need to Add parameter for the first route
Routes
Route::get('/products', [ProductController::class, 'index'])->name('product.index');
Route::get('/products/{slug}', [ProductController::class, 'show'])->name('product.show');
here in blade file
Low to High
High to Low
When you pass an array to the route helper it will try to match up the keys of that array to the route parameters (if there are any to match up), anything that isn't a route parameter gets added as a query string parameter; so that array isn't just for route parameters.
Your index route shows all elements which is not a specific element.
The show route expects a parameter to show you all details of exactly one item.
I am using Laravel 5.2.
I am passing parameters via two urls. But it always goes to the first route only.
code in my routes.php is as follows:
Route::get('{department}', 'settings#load_department_settings')->middleware('auth');
Route::get('{id}', 'settings#load_staff')->middleware('auth');
Anchor tags in view is as follows:
<td>{{$staff->name}}</td>
<td>{{$staff->deptname}}</td>
The issue is it always uses 'settings#load_department_settings'. How could i use 'settings#load_staff' when clicking on staff name?
It is always going to use the {department} route because that is matching anything on the root / slash. You need tto dive at least one of them unique prefixes. For example:
Route::get('department/{department}', 'settings#load_department_settings')->middleware('auth');
Route::get('staff/{id}', 'settings#load_staff')->middleware('auth');
Then
<td>{{$staff->name}}</td>
<td>{{$staff->deptname}}</td>
I have 2 routes:
/route/{id}
/route/add
First one shows something based on id provided and second one provies form for adding news item. My problem is that when I hit /route/add it takes the add word and treats it as id for the first route.
Is there any solution for this in laravel or do I have to use different route names?
Routes work on a first-come first-served basis. The earlier routes are looked at first. Since you have a wildcard {id} on your first of those two routes, Laravel is treating add as {id} and will be passing off to that controller/closure.
You should switch the two routes around like this:
Route::get('route/add', 'Controller#method');
Route::get('route/{id}', 'Controller#method');
OR you can always add a filter to the first route in order to tell Laravel that {id} should be a number:
Route::get('route/{id}', 'Controller#method')->where('id', '[0-9]+');
Route::get('route/add', 'Controller#method');
This way, Laravel will try to match add to your {id} wildcard but it will fail because add is not a number.
Please replace the two lines:
/route/add
/route/{id}
The {id} parameter is not always expecting a numeric value. A string is also valid like "add".
I'm having a problem with PyroCMS and CodeIgniter URI Routing.
I have a page (majors_list) has a child page (major) , which it has a child page too called (course).
$route['majors_list/major/(:any)'] = 'pages/view/majors_list/major';
$route['majors_list/major/(:any)/course/(:any)'] = 'pages/view/majors_list/major/course';
The first routing, is to view the major page which contains all the courses.
The second routing, conflicts with the first routing, and it is used to view the course information.
When I comment the first routing, the second routing works, but the first stops, and vice versa.
A real example:
majors_list/major/Dentistry/course/dental_material
You need to swap them around. To me it looks like any route that matches the second one will also match the first one, so it never reaches the second one, therefore swapping them will solve the issue.
Alternatively, you could use the regex syntax for routing and place a dollar sign at the end of the regex for the first route to exclude routes that continue after that point.
I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.