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
Related
So I'm trying to redirect with a session/flash variable, but I can't seem to figure out why the session variable is never present after the redirect.
I have a controller action like this:
public function verifyAction(Request $request)
{
return redirect()
->route('login')
->with('test', 'Test');
}
and on the login route I'm dumping the session (using Laravel's Session()->all() function), however it never seems to contain the 'test' key.
I'm using 'artisan route:list' to show the routes like this:
+--------+----------+--------+--------+---------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------+--------+---------------+------------+
| | GET|HEAD | login | login | #loginAction | web |
| | GET|HEAD | verify | verify | #verifyAction | web |
+--------+----------+--------+--------+---------------+------------+
and both routes are using the web middleware as specified in the docs ([laravel.com/docs][1]) but the session data is always empty after the redirect, like this:
["_flash"]=>
array(2) {
["old"] => array(0) {}
["new"] => array(0) {}
}
I'm expecting to see the 'test' key in the _flash array after the redirect but actually it's always empty.
Any ideas what's going on here and how I could get it working?
One thing that strikes me as strange is that I can add normal session variables in the controller like this and it works fine:
Session::put([
'test-2' => 'Testing'
]);
So session variables work fine, it's just flash variables that aren't working.
Distro: Ubuntu 19.04 (Disco Dingo), Laravel 5.8, Laradock, Docker version 18.09.5, build e8ff056, Php 7.2.15.
EDITS
Browser activity:
Request URL: http://localhost/verify?code=ABC&uid=2
Request Method: GET
Status Code: 302 Found
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade
Request URL: http://localhost/login
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade
I'm trying to get SSO working on an application, first time doing anything with this type of thing and falling at first hurdle.
Having some basic issues getting started here. Not totally sure where I am going wrong.
Trying to use https://github.com/aacotroneo/laravel-saml2; Running Laravel 5.4 on a development WAMPserver; installed the package fine, added provider and alias information to config/app.php as per all instructions.
If I try to publish the config file, I get no action, no error, just "Publishing Complete" in Composer.
I can copy the saml2_settings.php file to the config directory from the provider directory, and set the parameters there instead, however, no routes work - trying to get the metadata via /saml2/metadata URL just gives me a 404.
Any ideas - new to SAML but this seems just like a standard installation issue.
1) You need to add SAML2_IDP_HOST in env
2) Your url must contan prefix 'routesPrefix' => '/saml2', so your routes looks like below,
/**
* If 'useRoutes' is set to true, the package defines five new routes:
*
* Method | URI | Name
* -------|--------------------------|------------------
* POST | {routesPrefix}/acs | saml_acs
* GET | {routesPrefix}/login | saml_login
* GET | {routesPrefix}/logout | saml_logout
* GET | {routesPrefix}/metadata | saml_metadata
* GET | {routesPrefix}/sls | saml_sls
*/
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 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
I am trying to display information submitted from a form to create a message board. I am using php in the laravel framework. I am using a form, record, and repository. Whenever I try to display the content I receive the following error Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException.
Here is the controller:
/**
* Display a listing of the resource.
* GET /messages
*
* #return Response
*/
public function create()
{
return View::make('comments.create');
}
public function show($comment)
{
$message_id = $this->messageRepository->find($comment);
return View::make('comments.show')->with('comment', $message_id);
}
/**
* Store a newly created resource in storage.
* POST /messaages
*
* #return Response
*/
public function store()
{
$data = Input::all() ;
$this->messageForm->validate($data);
$messageRecord = new MessageRecord;
$messageRecord->comment = $data['comment'];
Return "Comment created";
}
}
Here is the view causing the trouble:
<p>$comment</p>
I have not been using a route but I threw this together:
Route::resource('/message', 'MessageController');
[Edit:]
If your store() is already used by another form and you still have another form to send POST data then, you should declare another routes for that. Like
Route::post('message/display', ['as' => 'message.display', 'uses' => 'MessageController#display');
Sorry for my previous answer, show() is for GET request and since you are using form you probably you don't need it unless you decide to send data through url. I'm extremely sorry.
[Edit ends]
You are getting that error because you are not allowed to use display() in resourceful routing. Instead, you must use store(). Since you are using laravel resourceful routing, you should follow the convention of laravel. In your command line, run
php artisan routes
You will get something like this,
GET|HEAD message | message.index | MessageController#index
GET|HEAD message/create | message.create | MessageController#create
POST message | message.store | MessageController#store
GET|HEAD message/{message} | message.show | MessageController#show
GET|HEAD message/{message}/edit | message.edit | MessageController#edit
PUT message/{message} | message.update | MessageController#update
PATCH message/{message} | | MessageController#update
DELETE message/{message} | message.destroy | MessageController#destroy
These are the methods, their route names and their corresponding HTTP request that you should use.
Check Laravel documentation for resourceful controller
For quick look,
index : Display a listing of the resource,
create : Show the form for creating a new resource,
store : Store a newly created resource in storage,
show : Display the specified resource,
edit : Show the form for editing the specified resource,
update : Update the specified resource in storage,
destroy : Remove the specified resource from storage
It seems you use incorrect verb for route displaying this route. For display you should probably use Route::get and you probably use Route::post. If it's not the issue you edit your question and put there routes.php as you were asked in comment.