Laravel define a put/patch route as the same route name - php

In Laravel it's quite handy to quickly generate a load of routes by using a route resource:
Route::resource('things'ThingsController');
This will produce all the necessary RESTful routes for CRUD operations. One of these is the PUT/PATCH route which might be defined as follows:
PUT/PATCH things/{id} ThingsController#update things.update
I've read around that it's better to explicitly define each of your routes rather than use a route resource but how would I define the PUT/PATCH route above. I understand that I can do
Route::put('thing/{id}', ['as' => 'things.update']);
or
Route::patch('thing/{id}', ['as' => 'things.update']);
But the second would overwrite or conflict with the first allowing the things.update route name to only refer to either a PUT or PATCH request. How can I explicitly create the combined PUT/PATCH route as created by the resource route?

After tedious searching, try the following;
Route::match(array('PUT', 'PATCH'), "/things/{id}", array(
'uses' => 'ThingsController#update',
'as' => 'things.update'
));
This allows you to restrict request via an array of Verbs.
Or you can limit the resource as so;
Route::resource('things', 'ThingsController',
array(
'only' => array('update'),
'names' => array('update' => 'things.update')
));
Both should provide the same result, but please note they are not tested.

This work for me
Route::match(['put', 'patch'],'thing/{id}', 'ThingsController#update');

Related

Laravel 5.5 Route Controller [ Page not found ]

I have a program that uses $router->resource([]). I use laravel-admin.
here my routes.php
$router->resources([
'programs' => ProgramController::class,
'programs/categories' => ProgramCategoryController::class,
]);
on my programs its work well with all the crud operation.
but on my programs/categories its not working, said not found. did route controller must use different url?...
i mean my category can't be child from my programs with different controller?...
Try changing "programs/categories" to "programs.categories"
You want to add "programs" prefix to categories resource routes. You can do it by changing you code as follows:
$router->resources([
'programs' => ProgramController::class,
]);
// to add programs prefix to categories routes
Route::group(['prefix' => 'programs'], function () use ($router) {
$router->resource('categories', ProgramCategoryController::class);
// here you can add more routes and all those routes will have
// "programs" prefix in there url
});
refer to https://laravel.com/docs/5.5/controllers#resource-controllers
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');
So on my case above, just simply change this:
$router->resources([
'programs' => ProgramController::class,
'programs/categories' => ProgramCategoryController::class,
]);
to this :
$router->resources([
'programs/categories' => ProgramCategoryController::class,
'programs' => ProgramController::class,
]);
and it's working well now, also both crud operations.
it's not an optimal solution but its working for me.

Laravel route starting with a specific string

I need to create a route with some fixed and dynamic parts. Basically I need to be flexible on the second segment on the url. If the url starts with 'products/test....' then the route has to go to the PageController, all other routes starting with 'products/....' have to go to the ProductController.
// Something like this:
Route::any('products/".starts_with($slug, 'test'), [
'uses' => 'PageController#show'
])->where('slug', '(.*)?');
Route::get('products/{slug}', [
'uses' => 'ProductController#show'
]);
Is this possible in Laravel 5?
In Laravel 5 we use Middleware as helpers for routes.
There are some examples in the default installation that you can adapt for your code. This is the best approach for this issue.

Laravel 5 custom named routes in resource controllers

How can I pass in my own extra named routes for a resource controller?
I have:
Route::resource('logistics', 'LogisticsController', ['names' => [
'index-inbound' => 'logistics.indexInbound'
]]);
But this does not work.
You can't really add additional routes to a resource route. However, you can add any other routes you want and point them to the same controller:
Route::get('logistics/inbound', ['name' => 'logistics.index-inbound', 'uses' => 'LogistictsController#indexInbound']);
Route::resource('logistics', 'LogisticsController');
Just make sure that you register your custom routes before the resource route as otherwise they might get overridden.

Laravel Route::resource naming

I have the following in my routes.php
Route::resource('g', 'GameController');
I link to these generated routes via HTML::linkRoute('g.index', 'Title', $slug) which produces a link to http://domain/g/demo-slug
What I am looking to do is verify if it is possible to have the prefix be declared in one place so I'm not hunting for links if a URL structure were to change.
For example, I would want to change http://domain/g/demo-slug to http://domain/video-games/demo-slug
I was hoping to use the similar functionality with the standard routes, but that does not seem to be available to resource routes.
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#getUpdated'));
Route::group() takes a 'prefix'. If you put the Route::resource() inside, that should work.
Tangent, I find this reads better:
Route::get('/', array('uses' => 'HomeController#getUpdated', 'as' => 'home'));
As far as I know it's true you can't have named routes for a resource controllers (sitation needed) but you can contain them in a common space using Route::group() with a prefix. You can even supply a namespace, meaning you can swap out an entire api with another quickly.
Route::group(array(
'prefix' => 'video-games',
'before' => 'auth|otherFilters',
'namespace' => '' // Namespace of all classes used in closure
), function() {
Route::resource('g', 'GameController');
});
Update
It looks like resource controllers are given names internally, which would make sense as they are referred to internally by names not urls. (php artisan routes and you'll see the names given to resource routes).
This would explain why you can't name or as it turns out is actually the case, rename resource routes.
I guess you're probably not looking for this but Route:group is your best bet to keep collections of resources together with a common shared prefix, however your urls will need to remain hard coded.
You can give custom names to resource routes using the following syntax
Resource::route('g', 'GameController', ['names' => [
'index' => 'games.index',
'create' => 'games.create',
...
]])
This means you can use {!! route('games.index') !!} in your views even if you decided to change the URL pattern to something else.
Documented here under Named resource routes

Required order for specifying restful routes in Laravel 4?

I'm trying to understand routing in Laravel 4. I read a good post here on StackOverflow and a link to beware the route to evil, a post about manually specifying routes. I like the idea of specifying my routes manually and having the routes.php act as documentation. But it seems like I need to be cautious about the order of my Routes if I'm going to specify my own instead of using Route::resource() If I have the new or create route before the show then I won't be routed to the show because of the variable in URI? The order in which the routes are defined is important right?
// This will not work if I try and browse to dogs/new
Route::get('dogs', array('as' => 'dogs', 'uses' => 'DogsController#index'));
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => 'DogsController#show'));
Route::get('dogs/new', array('as' => 'new_dog', 'uses' => 'DogsController#create'));
It seems I need to make sure that the dogs/new comes before the dogs/{dogs} for new to return correctly. I'm not clear on what {dogs} does or that's different from (:any) or {any} I've seen a few different uses in examples and pseudo code. I see that /new is the same as {...} when the route is before the more specific is the {} like a wildcard in Laravel 4? Is the (:...) the old way?
As an aside I've noticed a different naming convention from some of the examples I've seen when I run php artisan routes with a resource route like Route::resource('photos', 'PhotosController'); The method and named route for post to index to a create a new resource is named photos.store and #store. The method and named route for a link to a form to create a new resource is photos.create and #create. Is that Laravel 4 thing or conventions in other frameworks?
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => 'DogsController#show'));
The above url expecting a parameter after dogs segment.
for example: http://laravel.com/dogs/xyz, http://laravel.com/dogs/new
after dogs url segment, Laravel will accept anything. So, your another routing will never executed for the route parameter.
Route::get('dogs/new', array('as' => 'new_dog', 'uses' => 'DogsController#create'));
More about route parameters:
http://laravel.com/docs/routing#route-parameters
Resource Controllers
Laravel and Ruby on rails support resource full routing. I think, Tailor borrow the resource full routing idea from Ruby on rails.
The following routes will generate if you use resource controller:
index
create
store
update
show
edit
destroy
http://guides.rubyonrails.org/routing.html
http://laravel.com/docs/controllers#resource-controllers

Categories