Routing problem |
I have a problem with the routing that's mean the delete method.
In the app/routes.php i have : PHP Code: $routes->resource('department'); The documentatie says: PHP Code: $routes->resource('photos'); So if i have in my form the link to department/id like this: PHP Code: <?php echo form_open('department/'.$department_item['id'], 'method=delete'); ?> Code: <form action="http://cmms_rucphen.test/department/7" method="delete" accept-charset="utf-8"> The link is good but when i push the button it follows not the right route. He comes with a error but he seach for the "show"method and not the "delete" method. In my opinion is the code alright or not? Or is it a bug in Codeigniter 4? If i have time tonight i will change the route to apart routes. If that that works i will tell you.
Delete is not a valid method in HTML forms. Delete can be sent using XHR. HTML forms only support GET and POST
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
(01-17-2019, 08:30 AM)albertleao Wrote: Delete is not a valid method in HTML forms. Delete can be sent using XHR. HTML forms only support GET and POST Yes Yes now I now again in node.js they use method override. I wil now use the easy way like before. PHP Code: <a href="<?php echo base_url('department/delete/'.$department_item['id']) ?>" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</a> Works like a charm. I read the documents for the X time and read this. " The second parameter accepts an array of options that can be used to modify the routes that are generated. While these routes are geared toward API-usage, where more methods are allowed, you can pass in the ‘websafe’ option to have it generate update and delete methods that work with HTML forms:" PHP Code: $routes->resource('photos', ['websafe' => 1]); If I use the web safe it works also with "Post", I'm gonna test it over an hour.
You can use method spoofing in forms, though it appears that's not documented. Bad me. I just opened an issue at Github to make sure that gets done.
It would work like this: Code: <?php echo form_open('department/'.$department_item['id']); ?> (01-17-2019, 08:35 PM)kilishan Wrote: You can use method spoofing in forms, though it appears that's not documented. Bad me. I just opened an issue at Github to make sure that gets done. Sorry that won't work. I have tried many many times yesterday and one thing works. In the Routes: PHP Code: $routes->resource('department'); In the model: PHP Code: <?php In the form: PHP Code: <?php echo form_open('department/delete/'.$department_item['id']); ?> Also works: PHP Code: <?php echo form_open('department/delete/'.$department_item['id']); ?> In the controller: PHP Code: public function delete($id) So you can see that i added "delete/" in the form_open(). Without the "delete/" it wont work and takes the wrong method. The documentation says: PHP Code: $routes->delete('photos/(:segment)', 'Photos::delete/$1'); And this happens also for the update method. So i'm not a expert but what is wrong? Have i coded the wrong way or is it a bug or is the manual not correct? André |
Welcome Guest, Not a member yet? Register Sign In |