I've got 2 sections to my site, the admin side and the public side. The issue I'm having is that if I go to for example admin/menus then I go to my public side instead of going to the menus page.
I'm not sure why this is happening. I've tried to re-arrange the order of the routes in my public side but that didn't work and I've drawn a blank as to what I've done wrong.
My public routes
<?php
Route::get('/', [
'uses' => 'OpenController#index',
'as' => 'index',
]);
Route::get('/{id}', 'OpenController#content');
Route::post('/contact', [
'uses' => 'OpenController#contact',
'as' => 'contact',
]);
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
Route::any('/search', [
'uses' => 'OpenController#search',
'as' => 'search'
]);
my admin menus route
Route::resource('admin/menus', 'MenusController');
My productItem function
public function productItem($category, $slug)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$contact = Contact::all();
$single_product = Product::where('slug', $slug)->get();
return view('open::public.single_item', compact('menus_child', 'contact', 'single_product'));
}
The error come in with this route
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
If I remove this route then it works, but I need this route so I can't remove it.
If I'm missing something else that I need to give please let me know.
This will work if u put it at the top, but it may clash with other routes i think.
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
you can try like this if u want
Route::get('/category/{category}/{slug}', function (\Illuminate\Http\Request $request) {
echo "ok";
});
You should prefix the route /{category}/{slug} to avoid conflicts. So replace:
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
By:
Route::get('/open/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
And update your links to that route in your views.
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
Will catch every combination of path that have two items /admin/menus, /admin/anything or /foo/bar. You are probably going to run into the same problem with
Route::get('/{id}', 'OpenController#content');
If you can not rename your routes, you need to put all the more restrictive routes on top and your less restrictive routes on the bottom.
Route::resource('admin/menus', 'MenusController');
Route::get('/', [
'uses' => 'OpenController#index',
'as' => 'index',
]);
Route::post('/contact', [
'uses' => 'OpenController#contact',
'as' => 'contact',
]);
Route::any('/search', [
'uses' => 'OpenController#search',
'as' => 'search'
]);
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
Route::get('/{id}', 'OpenController#content');
UPDATE
You have a few options.
Here are two of them.
You can limit what the route will accept with RegEx.
See Route Parameters > Regular Expression Constraints
Route::get('/{category}/{slug}', function () {
return 'hello';
})->where('category', '[one]*[two]*[three]*[four]*[five]*');
Or you can change the caffeine route through its config.
php artisan vendor:publish --tag=genealabs-laravel-caffeine
Then change the route in /app/config/genealabs-laravel-caffeine.php
There are some other ways as well. I'd just up a quick test site and start messing with routes to see what works the best for your needs.
Related
I used this routes for either Laravel 5.1 and Laravel 5.3, and now when I'm using this type of route order it gives me the title error hope you can help me, you can find the code here :
Route::prefix('productos')->group(function () {
'as' => 'products.index',
'uses' => 'ProductController#index'
Route::get('crear',[
'as' => 'products.create',
'uses' => 'ProductController#create'
]);
Route::post('guardar',[
'as' => 'products.store',
'uses' => 'ProductController#store'
]);
// Editar, borrar
Route::get('{id}',[
'as' => 'products.destroy',
'uses' => 'ProductController#destroy'
]);
Route::get('{id}/editar',[
'as' => 'products.edit',
'uses' => 'ProductController#edit'
]);
Route::put('{id}',[
'as' => 'products.update',
'uses' => 'ProductController#update'
]);
});
To use => you need to be in the context of an associative array in php. In your case you are using it inside a closure:
Route::prefix('productos')->group(function () {
// This section is incorrect
'as' => 'products.index',
'uses' => 'ProductController#index'
// Because is not inside an array
Route::get('crear',[
'as' => 'products.create',
'uses' => 'ProductController#create'
]);
...
If I had to guess what you are looking for something like this:
Instead of
'as' => 'products.index',
'uses' => 'ProductController#index'
You should have something like:
Route::get('listar',[
'as' => 'products.index',
'uses' => 'ProductController#index'
]);
So the endpoint would be productos/listar.
Hope this helps you.
Syntax error
'as' => 'products.index',
'uses' => 'ProductController#index'
Change it like this
Route::get('products',[
'as' => 'products.index',
'uses' => 'ProductController#index'
]);
I'm using Laravel's route groups to try and stop duplication within my Routes file.
I have one main group, frontend. This has the namespace Frontend and as frontend.
Nested within that group, is another group. This group has the prefix account which appends /account/ to each route. It also has as account..
The routes inside the nested group, I'd expect to be:
frontend.account.home
frontend.account.order.show
frontend.account.order.index
Instead I get:
frontend.account.home
frontend.account.account.order.index
frontend.account.account.order.show
Code:
Route::group(['as' => 'frontend.', 'namespace' => 'Frontend'], function () {
Route::group(['prefix' => 'account', 'as' => 'account.', 'namespace' => 'Account'], function () {
Route::get('home', [
'as' => 'home',
'uses' => 'Home\Controller#get'
]);
Route::resource('order', 'Order\Controller', ['except' => [
'create',
'store',
'update',
'destroy',
'edit',
]]);
});
});
Since your excepting almost every routes from the Route::resource method, why not create 2 single routes for index and show like so:
// in your routes file, within your nested group :
Route::get('order', ['as' => 'order.index', 'uses' => 'Order\Controller#index' ]);
Route::get('order/{id}', ['as' => 'order.show', 'uses' => 'Order\Controller#show' ]);
In my application, I have the concept of events. Each event can be booked by a user, and also unbooked if that user needs to cancel.
My event routes look like this:
Route::get('events/search', 'EventsController#search');
Route::get('events/past', 'EventsController#past');
Route::get('events/{id}/invite', [
'as' => 'invite', 'uses' => 'EventsController#invite'
]);
Route::get('events/{id}/invite/groups', [
'as' => 'inviteGroups', 'uses' => 'EventsController#inviteGroups'
]);
Route::get('events/{id}/duplicate', [
'as' => 'duplicate', 'uses' => 'EventsController#duplicate'
]);
Route::get('events/{id}/book', [
'as' => 'book', 'uses' => 'EventsController#book'
]);
Route::get('events/{id}/unbook', [
'as' => 'unbook', 'uses' => 'EventsController#unbook'
]);
Route::post('events/{id}/groups', [
'as' => 'addGroups', 'uses' => 'EventsController#addGroups'
]);
Route::post('events/{id}/helpers', [
'as' => 'addHelpers', 'uses' => 'EventsController#addHelpers'
]);
Route::resource('events', 'EventsController');
I'm having difficulty with the "book" route, which throws the following error when I hit it:
MethodNotAllowedHttpException in RouteCollection.php line 201:
The route users a function that attaches a user to an event, and then returns that user to the event page.
I'm sure its an order of precedence issue, but no matter how I organize the routes, I keep hitting that error. This is the only route on the list that's causing problems. Are there any best practices on route organization to fix this issue?
I have a user controller which returns users list, each user belongs to different group.
ex:- $groups = ['student_users' => 1, 'teacher_users' => 2, .....]
I can create routes such as below to access them
Route::get('users/{id}', [
'as' => 'user',
'uses' => 'Admin\UserController#listUser'
]);
But i want to create more user friendly or seo friendly say like this
Route::get('users/student', [
'as' => 'student',
'uses' => 'Admin\UserController#listUser'
]);
Route::get('users/teacher', [
'as' => 'teacher',
'uses' => 'Admin\UserController#listUser'
]);
Route::get('users', [
'as' => 'student',
'uses' => 'Admin\UserController#listUser'
]);//by default shows students list.
And i want to pass the id via route not via url. Whats the best way to do it.
do as following
Route::get('users/student', [
'as' => 'student',
'type' => 'student',
'uses' => 'Admin\UserController#listUser'
]);
in controller you can get type as below
public function listUser(\Illuminate\Http\Request $request)
$action = $request->route()->getAction();
dd($action['type']);
}
type is just an example. You can pass any variable.
I hope it helps.
I'm trying to accomplish the to control my function by the follwoing routes:
Route::get('tri/{uniquename}/photos/gallery/{pic}', array( 'as' => 'sportevent', 'uses' => 'SporteventController#thisevent'));
Route::get('tri/{uniquename}/{tab}/{filter}/', array( 'as' => 'sportevent', 'uses' => 'SporteventController#thisevent'));
Route::get('tri/{uniquename}/{tab}/', array( 'as' => 'sportevent', 'uses' => 'SporteventController#thisevent'));
I'm aware, that I could combine route 2 and 3 to
Route::get('tri/{uniquename}/{tab}/{filter?}/', array( 'as' => 'sportevent', 'uses' => 'SporteventController#thisevent'));
but that's not really my problem here.
my function looks as follows (only the relevant code):
public function thisevent($uniquename, $tab="main", $filter="",$pic=""){
if($pic!=""){
$tab = "photos";
}
.....
}
The function does not detect the $pic parameter if I request an URL like this:
http://dev.hobbyathletes.com/tri/Ocean-Lava-Lancerote-Triathlon-2014/photos/gallery/6
what am I doing wrong here?
this is right :
Route::get('tri/{uniquename}/{photos}/{gallery}/{pic}', array( 'as' => 'sportevent', 'uses' => 'SporteventController#thisevent'));
in your code the Laravel think you have two arguments and $tab argument will be as $pic