Welcome Guest, Not a member yet? Register   Sign In
RESTful and auto routing improved
#1

(This post was last modified: 01-20-2023, 07:01 PM by Rinart.)

I have a controller App/Controllers/Api/Orders.php
Code:
<?php

namespace App\Controllers\Api;

use CodeIgniter\RESTful\ResourceController;

class Orders extends ResourceController
{
    protected $modelName = 'App\Models\Order';
    protected $format    = 'json';

    public function getIndex()
    {
        return $this->respond($this->model->findAll());
    }

    public function delete($id = null)
    {
        return $this->respond(['test' => 123]);
    }
}
If I'm trying to access /api/orders/ with GET request it works fine (so auto routing is working).
I'm trying to send a DELETE request: /api/orders/15 however I'm getting error 404. What am I doing wrong?
Reply
#2

PHP Code:
/api/orders/delete/15 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

PHP Code:
public function deleteIndex($id null
Reply
#4

Sorry, you can't use parameters with *index() method.
DELETE /api/orders/15 is routed to Orders::delete15().
Reply
#5

So basically Auto Router Improved doesn't support the example from the Docs and the way to go is to define routes for api manually?
Code:
$routes->resource('photos');

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

No, Auto Router Improved does not support the same URIs that $routes->resource() provides.
Reply
#7

I sent a PR to improve it.
https://github.com/codeigniter4/CodeIgniter4/pull/7162

How do you think?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB