CodeIgniter Forums
Resource Controller problem with show method and uri param - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Resource Controller problem with show method and uri param (/showthread.php?tid=81831)



Resource Controller problem with show method and uri param - kabeza - 05-05-2022

I have the following route configured in app/Config/Routes.php

Code:
$routes->resource(
    '/api/project',
    [
        'only' => ['index', 'show'],
        "controller" => "Api\Project"
    ]
);

Then I have the Project controller in app/Controllers/Api/Project.php

Code:
<?php
namespace App\Controllers\Api;
use CodeIgniter\RESTful\ResourceController;
class Project extends ResourceController
{
    protected $modelName = 'App\Models\ProjectModel';
    protected $format    = 'json';

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

    public function show($id = null)
    {
        return $this->respond($id);
    }
}

When I run
http://myapp/api/project

I get all the records in json, all works fine

But when running this url
http://myapp/api/project/show/26

I get as output show and not 26 value.

What could be happening ?

How should I modify the route in case show method has more parameters?

Thanks a lot

Well

I must have looked like an a*****e

For anyone having similar "issue" the correct url for show method in resourceController is

http://myapp/api/project/26

Because looking several times the way routes are generated, the show route is generated without the show word

https://codeigniter.com/user_guide/incoming/restful.html#id4