• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Routing problem

#1
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.
Reply

#2
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
Reply

#3
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!
Reply

#4
(01-17-2019, 09: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.
Reply

#5
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(); ?>
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#6
(01-17-2019, 09: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é
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.