Laravel Routing Parameter Contraint - php

Been working on a neat little project. I'm using Laravel 5.5, and I'm building routes to handle the various requests. I've got a route that accepts a slug to locate a particular guild via route model binding. Works great! Beautifully, actually. Then, I defined a "static" route that doesn't use a parameter to show the form for creating a new guild. Here's the routes...
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
But when I attempt to navigate to '/guild/create', I get a 404 because a guild with the slug "create" doesn't exist. How can I work around this particular issue?

Try put specific routes first:
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guil');

Related

Laravel returning the same view for 2 different routes

Laravel is somehow getting the same view for 2 different routes and I can't figure out why since I'm not very familiar with Laravel.
Here's the 2 routes in question :
Route::get('/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/{id}', [BookController::class, 'singlebook'])->name('book');
The first route is the one returning the view (calling the method), that means if I switch between them it wall call singlebook() in both routes.
HTML :
Book
Category
When you try to access any resource you gonna write the route on the browser
so what if i just had two resources like those:
Route::get('/book/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
And i am trying to access localhost::8080/book/5
So what tells laravel from this route that this route belongs to which one of the resources ?
Nothing !!
Cause 5 could be considered as a label or id
So you should change the route to be identifiable to laravel
Try to change one of the routes, Example:
Route::get('/book/by/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
Should be obvious that when there is no other way for laravel to determine which route you want, that it will route to the first one it has in its routing table.
This would be a simple workaround that should illustrate what you are missing.
Route::get('/cat/{label}', [BookController::class, 'listbyCat'])-name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
Artisan should be your goto tool whenever you first encounter a problem like this.
php artisan route:list

Laravel Duplicate Route names

I'm using same route name for the get & post methods in route. those routes are using for the same purpose.
ex : I'm calling to load add view form using get route
Route::get('add', 'UserController#addView')->name('user.add');
then,
I'm calling to store data in that form using post route
Route::post('add', 'UserController#store')->name('user.add');
is there any issue , If I use the same route name like this?
No, you can not use the same name for 2 different routes as is stated in the documentation if you really need to name the routes you should look for different names, but if there's no need to have named routes you can have each url with its method like:
Route::get('/your-url', 'App\Http\Controllers\UserController#addView');
Route::post('/your-url', 'App\Http\Controllers\UserController#store');
If you are making a CRUD you can have:
Route::resource('user', UserController::class);
This will create all the urls needed for a CRUD:
Actually you have same name for both routes i.e., name('user.add'). Change that.
Yes you can get issues in future. For example, I had issues after functionality update - I've updated route interface (added some fields) and new fields does not appeared in result after opening URL.
Please run command php artisan optimize to see is it all ok.

Laravel Route goes to Controller but does not display data

I created the following route
Route::resource('posts','PostController');
Now I'm trying to create a custom route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
When the link posts/trashed is clicked, it goes to the PostController#trashed, but the controller shows blank page without any errors.
So changed the custom route to
`Route::get('post/trashed','PostController#trashed')->name('posts.trashed');
by simply changing the posts/trashed to post/trashed and the controller works fine.
Can someone explain what the issue is.
Figured it out. Solution was hidden deep inside the Laravel Documentation . https://laravel.com/docs/5.0/controllers#restful-resource-controllers
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:
Re-ordering will solve the issue.
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Route::resource('posts','PostController');
The resource route has some routes inside it own.
So this route:
Route::resource('posts','PostController');
contains this route :
Route::get('posts/{post_id}',PostController#show)
So it has conflict with your route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Solution 1:
bring your route upper than resource
I mean:
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed'); <-- first
Route::resource('posts','PostController');
Solution 2:
As you mentioned use another url (like post/trushed)
Solution 3 :
Add exception for resource.
Route::resource('posts', 'PostController')->except([
'show'
]);

Get route pattern by route name in laravel 5.3

In laravel 5.1, I was able to get the route path by route name, for example:
Defined Route:
Route::post('users/{user_id}/delete', 'UserController#delete')->name('user:delete');
In laravel 5.1, when I tried the below method, It gave the route without any error If I didn't pass any route parameter:
route('user:delete'); // Output: http://example.com/users/%7Buser_id%7D/delete
and then in javascript, I simply replaced %7Buser_id%7D with the user id dynamically. But laravel 5.3 is throwing error when accessing route by name that has parameters and I don't want to pass one, because the parameters are set dynamically from the javascript.
Is there any way I can access route pattern by route name like:
http://example.com/users/{user_id}/delete
Or
/users/{user_id}/delete
Thanks in advance.
You can give some route method some value, that will be then replaced in javascript. For example: route('user:delete', 'USER_ID'), then in javascript you will simply replace USER_ID.
or the better way, is to use package called "Laroute"

Route::controller with optional parameters in Laravel 4

I'm just new to Laravel but I immediately fell in love with it. As a not so super experienced php developer I do find the official documentation, although very expansive, somewhat complicated to use and find everything I need.
My question is about the Routing component. As the documentation states you can assign a route to a controller with the Route::controller method. So if I want a Blog controller for all /blog/ routes I assign it like this:
Route::controller('blog', 'BlogController');
So then if I'd like to acces all my blog posts I acces the the getIndex method by www.foo.com/blog or www.foo.com/blog/index
But let's say I'd like to be able to display categories via a getCategory method. My url would look like www.foo.com/blog/category and if, for example, I want to get the news category from the DB by slug, I'd like to use: www.foo.com/blog/category/news as the URI.
My question now is, how do I pass the slug to the url and access it in the getCategory method? Do I need specify it via Route::get('blog/category/{slug}', 'BlogController#getCategory') or is there a way to use Route::controller('blog', 'BlogController') and to send and acces parameters from the URL in the getCategory method?
I already tried to find it via google and in the official documentation, but I couldn't find a crystal clear answer to this problem...
You can simply add parameters to your getCategory method:
public function getCategory($category) {
die($category);
}
If you initialize it to null in the parameter list, it becomes optional. Alternatively, you can always pull parameters from the Input object but they would need to be passed in querystring format:
$category = Input::get('category');
With that said, I'd caution against using the Controller route. It's handy and mimics traditional MVC frameworks, but I believe it's planned to be deprecated -- and honestly, you miss out on some pretty flexible features.
using Route::controller('blog', 'BlogController'); allows you to define a single route to handle every action in a controller using REST naming conventions.then you have to add methods to your controller, prefixed with the HTTP verb they respond to. That means if you have a method called getIndex() it will be executed when there is a GET request to the url "yoursite.com/blog".
To handle POST requests to the same url add a method prefixed with post(ex: postComment()) and so on for other http verbs PUT, PATCH and DELETE.
I think you want something more customized, so you can use a resource controller:
Route::resource('blog', 'BlogController');
This will generate some RESTful routes around the blog resource, run php artisan routes in your project folder to see the generated routes, it should be something like this:
Verb Path Action Route Name
GET /blog index blog.index
GET /blog/create create blog.create
POST /blog store blog.store
GET /blog/{blog} show blog.show
GET /blog/{blog}/edit edit blog.edit
PUT/PATCH /blog/{blog} update blog.update
DELETE /blog/{blog} destroy blog.destroy
in the action column are the functions that you should have in the controller.
If you want to define more routes you can simply do it with Route::get or Route::post in the routes.php file
I hope this will make it more clear for you, enjoy routing with Laravel!!!

Categories