Place a resource controller at root of website? - laravel

How can I use a resource controller at the root of my website? I have tried:
Route::resource('/', CategoryController::class);
But no luck.

Normally, you wouldn't do this as the first argument to that method is a resource name, not a path. Though, you can do this if you really want to by overriding the name of the route parameter that ends up getting used:
Route::resource('/', YourController::class)->parameters(['' => 'category']);
This would create the routes with a parameter named 'category':
GET {category} show
GET {category}/edit edit
PUT|PATCH {category} update
...
If you wanted to you could also update the names of these routes if needed via the names method.

Related

Broken route in Laravel depending on where it's defined in the file

I have those routes defined in my routes/web.php :
Route::get('references/', 'referenceController#index')
Route::get('references/{reference}', 'referenceController#show')
Route::get('references/create', 'referenceController#create')
Like that, the references/create route goes to a 404 page.
If I put this route one line before, everything works fine :
Route::get('references/', 'referenceController#index')
Route::get('references/create', 'referenceController#create')
Route::get('references/{reference}', 'referenceController#show')
Then it is obviously because of the {reference} part in my route, right? But as I wanted to filter the reference perfectly, I've put a pattern in RouteServiceProvider.php. This pattern should check that my reference is a well-formed UUID :
Route::pattern('reference', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{10}');
Miles away from the word "create", which doesn't match the pattern.
Do you know why my route is going to a 404 page depending on its position in the file?
This is how Laravel is supposed to work. It isn't very clear in the documentation though I'll admit.
Supplementing Resource Controllers
If you need to add additional
routes to a resource controller beyond the default set of resource
routes, you should define those routes before your call to
Route::resource; otherwise, the routes defined by the resource method
may unintentionally take precedence over your supplemental routes:
Route::get('photos/popular', 'PhotoController#method');
Route::resource('photos', 'PhotoController');
This is also true if you are defining non-resource routes as you noted in your example. This is because it will try to pass "create" as the id of the reference parameter in the route which of course is not valid.
Rule of Thumb
When defining routes that have the same number of url segments, always define the route that does not have a parameter variable first. The routes file will go top-down and find the first route that matches the current request.

LARAVEL ROUTES not accept

and it works:
im add route and have view:
and errors:
wtf??? only copy paste, and rename...
Check layouts/app.blade.php There's something like route('qwe') and this route doesn't exist at all. So delete it from the layout and it should work.
Update
You're calling route('kwe') but you didn't name the route in the file. so add a name method to your route like the following.
Route::get('kwe', function(){
return view('kwe');
})->name('kwe');
Update 2
route helper's parameter is the route name not its path. So you need to name the route before calling it using route helper.
If you wanna use the path itself instead you can use url helper.

How to declare routes with resources in Laravel 5.2

I have some routes in routes.php in laravel
// Code for rounting admin panel
Route::resource('/admin','Admin\LoginController#index');
Route::resource('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movies','Admin\MovieController#index');
Now when I access url http://localhost/askspidy/admin I want to show login page and it works, but when i access url http://localhost/askspidy/admin/dashboard it should go to dashboard but it's showing me login page only.
I know this is because when it found /admin in any url it's bydefault goes to the route
Route::resource('/admin','Admin\LoginController#index');
I know it's assuming that (/admin) is route to controller and (/dashboard) is the function declared in the controller but I want routing like this only so is there any other solution for this problem.
A RESTful Resource Controller takes over the responsibility of each action. You only need to list the name and the controller:
Route::resource('photo', 'PhotoController');
If you wanted to only use the index method, you’d list it like this:
Route::resource('photo', 'PhotoController', ['only' => [
'index'
]]);
However, it looks like two of your routes are not suitable for resources (login and dashboard), as they should relate to editing a model.
You should instead just use a get() resource instead.
From the docs:
Route::get('user/{id}', 'UserController#showProfile');
So in your case, it would be:
Route::get('/admin','Admin\LoginController#index');
Route::get('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movie','Admin\MovieController');

Laravel 5.2 blade route to resource

The documentation says:
If the named route defines parameters, you may pass the parameters as
the second argument to the route function. The given parameters will
automatically be inserted into the URL in their correct positions:
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
//
}]);
$url = route('profile', ['id' => 1]);
If i do have a nested resource route by using Route::resource(...) twice, i will get a named route wich contains a placeholder like
employees.{employee}.images.index
How can i create a route for this nested resource using the blade templating engine?
I thought about
route('employees.{employee}.images.index', ['employee' => $employee->id]);
but that does not work. I know i can "manually" create the routes, but this will make them less maintainable.
Update 1
I know i can name the routes manually and then use the given name. But if there is a way without naming them i would prefer it.
You don't have to add anything like employee into a route name. Run php artisan route:list command and you will see real names of all routes (look at column called Name), created by resource clause. Then just use them like usual:
route('employees.images.index', ['employee' => $employee->id]);
Also, you can name resource routes.

how to pass variable from view to route in laravel

How to pass a variable from view to route in Laravel?
Here's the code at my route.php:
Route::get('/{id}/{id1}', 'WelcomeController#index');
And at welcome.blade.php:
< a href="{{URL::route('/{4}/{5}')}}">test</a>
I want to build a link referencing the route above.
A good approach to do it would be to name your route and then reference it by name, passing the parameters needed. Here is how:
At routes.php:
Route::get('/{id}/{id1}', ['as' => 'welcome_index', 'uses' => 'WelcomeController#index']);
And at your view, you can do this:
test
Notice that the first parameter represent the route name and the second, the parameters for your URL. You can read more here.
The advantages of naming a route is that you can change your route path later and your URL will still work with the code above.

Resources