CodeIgniter Forums
Routing problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Routing problem (/showthread.php?tid=72641)



Routing problem - snelledre - 01-17-2019

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');

// Equivalent to the following:
$routes->get('photos'                'Photos::index');
$routes->get('photos/new'            'Photos::new');
$routes->get('photos/(:segment)/edit''Photos::edit/$1');
$routes->get('photos/(:segment)'     'Photos::show/$1');
$routes->post('photos'               'Photos::create');
$routes->patch('photos/(:segment)'   'Photos::update/$1');
$routes->put('photos/(:segment)'     'Photos::update/$1');
$routes->delete('photos/(:segment)'  'Photos::delete/$1'); 

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'); ?>
    <button type="submit" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</button>
<?php echo form_close(); ?>
In the browser you see then.
Code:
<form action="http://cmms_rucphen.test/department/7" method="delete" accept-charset="utf-8">
   <button type="submit" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</button>
</form>

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.


RE: Routing problem - puschie - 01-17-2019

im not sure if "delete" is avaible in html forms ( guess js only ) - Ref

quick test : doenst work at all -> need to manual set the request_verb in php


RE: Routing problem - albertleao - 01-17-2019

Delete is not a valid method in HTML forms. Delete can be sent using XHR. HTML forms only support GET and POST


RE: Routing problem - snelledre - 01-17-2019

(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.  Rolleyes 
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]);

// The following equivalent routes are created:
$routes->post('photos/(:segment)'       'Photos::update/$1');
$routes->post('photos/(:segment)/delete''Photos::delete/$1'); 

If I use the web safe it works also with "Post", I'm gonna test it over an hour.


RE: Routing problem - kilishan - 01-17-2019

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']); ?>
    <input type="hidden" name="_method" value="delete" />
    <button type="submit" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</button>
<?php echo form_close(); ?>



RE: Routing problem - snelledre - 01-18-2019

(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.

It would work like this:

Code:
<?php echo form_open('department/'.$department_item['id']); ?>
   <input type="hidden" name="_method" value="delete" />
   <button type="submit" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</button>
<?php echo form_close(); ?>

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
use CodeIgniter\Model;

class 
DepartmentModel extends Model
{
protected 
$table      'department';
protected 
$primaryKey 'id';

protected 
$returnType 'array';
protected 
$useSoftDeletes true;

protected 
$allowedFields = ['name'];

protected 
$useTimestamps true;
protected 
$dateFormat 'datetime';

protected 
$validationRules    = [];
protected 
$validationMessages = [];
protected 
$skipValidation     false;


In the form:
PHP Code:
<?php echo form_open('department/delete/'.$department_item['id']); ?>
    <button type="submit" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</button>
<?php echo form_close(); ?>

Also works:
PHP Code:
<?php echo form_open('department/delete/'.$department_item['id']); ?>
    <input type="hidden" name="_method" value="delete" />
    <button type="submit" class="button is-small btn-align accent-btn raised rounded btn-outlined">delete</button>
<?php echo form_close(); ?>

In the controller:
PHP Code:
public function delete($id)
{
 
   $model = new \DepartmentModel();

 
   $model->delete($id);
 
   return redirect()->to('/department');


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é