I have tournament > category > setting, and I need to edit the settings.
For the creation ( http://laravel.dev:8000/tournaments/1/categories/5/settings/create
) , I have no problem, only updating is failing ( http://laravel.dev:8000/tournaments/1/categories/2/settings/5/edit )
I checked the params (1,2,5), and they are OK.
I use my route with resource()
Route::resource('tournaments/{tournamentId}/categories/{categoryId}/settings', 'CategorySettingsController');
When I type php artisan route:list, I get this route:
GET|HEAD | tournaments/{tournamentId}/categories/{categoryId}/settings/{settings}/edit | tournaments.{tournamentId}.categories.{categoryId}.settings.edit | App\Http\Controllers\CategorySettingsController#edit | auth,roles |
So, as for me, everything should be OK, I don't understand why I get a NotFoundHttpException
Any idea????
in RouteServiceProvider.php, I had binding defined:
$router->model('settings','App\Settings');
So General settings bindings was conflicting with Category Settings
Related
I have a standard resource controller registered like this:
Route::resource('/movies', \App\Http\Controllers\MovieController::class );
I checked php artisan route:list and I have the get 'movies' route.
But in blade template
Movies
does not work anymore.
And only
Movies
works.
Before that when I had:
Route::get('/movies', function () {
return view('movies');
})->name('movies');
The route method worked.
So url() method works everywhere and route() only in specific cases.
Could anyone tell me what is the main difference between those two methods?
which one should be used when?
And the main question - is it OK that the route() method does not work with resource controller, or is it something wrong with my code?
And since url() works everywhere should I just forget about route() method as unreliable?
If you look at the result of php artisan route:list you will see several routes e.g.
| | POST | movies | movies.store | App\Http\Controllers\MovieController#store | web |
| | GET|HEAD | movies | movies.index | App\Http\Controllers\MovieController#index | web |
| | DELETE | movies/{movie} | movies.destroy | App\Http\Controllers\MovieController#destroy | web |
| | PUT|PATCH | movies/{movie} | movies.update | App\Http\Controllers\MovieController#update |
route() and url() accept different values.
route() will accept route names e.g. movies.index, movies.destroy, movies.update and
url() will accept movies, movies/23.
route() will select the right methods (GET, POST, PATCH, DELETE) for you but
url() will only provide the path.
The Laravel application I am working on has two resources.
The routes for the second resource are given below:
$ php artisan route:list | grep -i activity
POST | admin/procedure/{id}/activity | admin.procedure.{id}.activity.store | (...)\ProcedureActivityController#store
GET|HEAD | admin/procedure/{id}/activity | admin.procedure.{id}.activity.index | (...)\ProcedureActivityController#index
GET|HEAD | admin/procedure/{id}/activity/create | admin.procedure.{id}.activity.create | (...)\ProcedureActivityController#create
GET|HEAD | admin/procedure/{id}/activity/{activity} | admin.procedure.{id}.activity.show | (...)\ProcedureActivityController#show
PUT|PATCH | admin/procedure/{id}/activity/{activity} | admin.procedure.{id}.activity.update | (...)\ProcedureActivityController#update
DELETE | admin/procedure/{id}/activity/{activity} | admin.procedure.{id}.activity.destroy | (...)\ProcedureActivityController#destroy
GET|HEAD | admin/procedure/{id}/activity/{activity}/edit | admin.procedure.{id}.activity.edit | (...)\ProcedureActivityController#edit
I call this setup a nested resource because activities are defined under a procedure. The definition or the routes looks like this:
Route::resource('procedure', 'ProcedureController');
Route::resource('procedure/{id}/activity', 'Admin\ProcedureActivityController');
I would like to generate a link to the POST action for a new activity that belongs to procedure 3 as I would with the list-all-procedures route;
$ php artisan tinker
>>> route('admin.procedure.index')
=> "http://localhost/admin/procedure"
>>> route('admin.procedure.{id}.activity')
InvalidArgumentException with message
'Route [admin.procedure.{id}.activity] not defined.'
Is there a way to generate a link to a nested resource using the standard helpers and facades?
Your route definition for the nested resource is not quite right.
Route::resource('procedure/{id}/activity', 'Admin\ProcedureActivityController');
Should be:
Route::resource('procedure.activity', 'Admin\ProcedureActivityController');
Also I am not sure how you are getting {id} in the URI as the ResourceRegistrar will create parameters based on the resource name. Based on the definition that should be {procedure} for your first resource definition.
You should end up with a route name like admin.procedure.activity.index for the index route.
route('admin.procedure.activity.index', ['procedure' => $id]);
Laravel 5.1 - Controllers - Restful - Nested Resources
Route::resource('photos.comments', 'PhotoCommentController');
This route will register a "nested" resource that may be accessed with URLs like the following: photos/{photos}/comments/{comments}.
You should use route() with a parameter to make it work:
route('admin.procedure.{id}.activity.index', $id);
I'm building a RESTful API System with CakePHP 3.1.13 ( i can't use 3.2.x because the Server PHP Version is 5.5.x ).
My controller name is CmsCouplesController.php and the url :
http://localhost/~emanuele/works/grai/html/api/v1/cms-couples.json
works correctly.
BUT the other call ( http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/1.json ) return :
Action CmsCouplesController::1() could not be found, or is not accessible.
If i create a controller CouplesController.php all works fine.
So why?!
UPDATE : routes configuration
Router::scope('/', function ($routes) {
$routes->prefix('v1',function($routes) {
$routes->extensions(['json','xml']);
$routes->resources('Couples');
$routes->fallbacks('DashedRoute');
});
Resource routes require separate inflection configuration
You are missing the proper inflection configuration for your resource routes. By default resource routes are using underscore inflection, ie currently your resource routes will match cms_couples.
Note that you can easily check which/how routes are connected by using the routes shell
bin/cake routes
It will show you something like
| v1:cmscouples:index | /v1/cms_couples | {"controller":"CmsCouples","action":"index","_method":"GET","prefix":"v1","plugin":null} |
| v1:cmscouples:add | /v1/cms_couples | {"controller":"CmsCouples","action":"add","_method":"POST","prefix":"v1","plugin":null} |
| v1:cmscouples:view | /v1/cms_couples/:id | {"controller":"CmsCouples","action":"view","_method":"GET","prefix":"v1","plugin":null} |
| v1:cmscouples:edit | /v1/cms_couples/:id | {"controller":"CmsCouples","action":"edit","_method":["PUT","PATCH"],"prefix":"v1","plugin":null} |
| v1:cmscouples:delete | /v1/cms_couples/:id | {"controller":"CmsCouples","action":"delete","_method":"DELETE","prefix":"v1","plugin":null} |
Long story short, use dasherize inflection and you should be good.
$routes->resources('CmsCouples', [
'inflect' => 'dasherize'
]);
See also
Cookbook > Shells, Tasks & Console Tools > Routes Shell
Cookbook > Routing > URL Inflection for Resource Routes
API > \Cake\Routing\RouteBuilder::resources()
from my understanding...
http://localhost/~emanuele/works/grai/html/api/v1/cms-couples.json
must be pointing to index function of CmsCouplesController.php controller
then for what reason you want this kind of url
http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/1.json
you can give url like
http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/json-request
then you can put your code in jsonReuest function of the CmsCouplesController.php controller ...
If this does not help then explain your question with answer of my question of why you want URl like 1.json
I am having a problem with the laravel5 resource controller. The POST method is working fine however the delete method is not. as you can see from postman i am passing the DELETE _method to the correct route
In the mean time i am using direct routes which are also working fine.
Route::delete('customisemymeal', ['as'=>'customisemymeal', 'uses'=>'UserMealCustomController#destroy']);
Route::post('customisemymeal', ['as'=>'customisemymeal', 'uses'=>'UserMealCustomController#store']);
I have disabled the CSRF token check until this is sorted out.
Can you please help explain why the same method is different for a resource controller compared to a route::delete?
routes:list
| DELETE | customisemymeal/{customisemymeal} | customisemymeal.destroy | App\Http\Controllers\UserMealCustomController#destroy |
| DELETE | customisemymeal | customisemymeal | App\Http\Controllers\UserMealCustomController#destroy |
To use the route:
Route::resource('customisemymeal', ['as'=>'customisemymeal', 'uses'=>'UserMealCustomController']);
You must abide to a few rules. To delete something you need to use:
domain.com/customisemymeal/resource_id
From your screenshots you are trying to delete a resource, using a different URI.
domain.com/customisemymeal
That won't work.
Rules are:
Index:
GET -> domain.com/resource
Show:
GET -> domain.com/resource/resource_id
create:
GET -> domain.com/resource/create
edit:
GET -> domain.com/resource/resource_id/edit
update:
PATCH / UPDATE -> domain.com/resource/resource_id
store:
POST -> domain.com/resource
delete:
DELETE -> domain.com/resource/resource_id
I'm creating my custom modules for my projects to be able to add some features to a project or another depending of the requeriments.
My issue is with the routes, I load the routes in a ModuleServiceProvider loaded in app.php:
include __DIR__.'/../../modules/canae/Http/routes.php';
I checked that this works with an echo inside that file. The routes.php file contains the following code:
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
Route::controller('dogs', 'Canae\Http\Controllers\Admin\DogController');
});
I also checked that Laravel can find the Controller, the problem is that it's unable to execute the code inside it.
Here's the code I have inside DogController:
<?php namespace Canae\Http\Controllers\Admin;
class DogController extends \Origin\Http\Controllers\Controller {
public function getIndex() {
echo "Hello!";die();
}
}
And the error is Controller method not found.
If I modify the extends below to Origin\Http\Controllers\Controller (removing the first \) I get the following error: Class 'Canae\Http\Controllers\Admin\Origin\Http\Controllers\Controller' not found so my conclusion is that the code inside this controller is executing, at least reading from Laravel.
Also I'm trying to achieve the Index function with this route http://localhost/canae/public/admin/dogs/index.
This is the tail result of executing php artisan route:list:
| | GET|HEAD | admin/dogs/index/{one?}/{two?}/{three?}/{four?}/{five?} | | Canae\Http\Controllers\Admin\DogController#getIndex | auth |
| | GET|HEAD | admin/dogs | | Canae\Http\Controllers\Admin\DogController#getIndex | auth |
| | GET|HEAD|POST|PUT|PATCH|DELETE | admin/dogs/{_missing} | | Canae\Http\Controllers\Admin\DogController#missingMethod | auth |
+--------+--------------------------------+-------------------------------------------------------------------------------+--------+--------------------------------------------------------------------+------------+
Tell me if you need more information. And thanks for your time.
I solved it moving the line inside providers that loads this routes to the first item of the providers array, even before the application ones. Don't know why, but now it's working.
In light of the docs at: http://laravel.com/docs/master/controllers
Have you tried using the "use" statement? Your code would then look like:
<?php
namespace Canae\Http\Controllers\Admin;
use Canae\Http\Controllers\Controller;
class DogController extends Controller {
public function getIndex() {
echo "Hello!";die();
}
}
I'm also not sure why your namespace is "Canae\Http\Controllers\Admin" as the example shows "App\Http\Controllers" only. I'm not familiar with the specific structure of your project, but removing the "\Admin" might also help.