Example demo error : Cannot access the controller in Defined Routes. Controller: \\App\\Controllers\\Example

Hello, I’m using GCE with CI4 version 4.2.11. I’m testing the “example” demo.

I’m using the autoRoutesImproved config and the routes are defined like in the tutorial, but the URL example/customers shows a 404 error in a modal.

When I change the name of the “customers” method in the Example controller, I get the CI4 404 error page, “method is not found”. So the errors seems to be after the controller loading.

When I don’t use the AutoRoute, the error message changes : “message”: “Can’t find a route for ‘get: example/customers&action=initial’.”,

I’m stuck, thanks for any help :slight_smile:

Hello @ljegou and welcome to our forums :hugs:

First of all, thank you for the detailed report since it is easier for me to help you. In order to solve the issue that you have it is better to has the AutoRoute as false and add the Routes manually. For example for your case you can add the following into your Route file (app/Config/Routes.php):

// Make sure that you always add get and post functions
$routes->get('/example/customers', 'Example::customers');
$routes->post('/example/customers', 'Example::customers');

and it should work for you.

Let me know if you are still having issues.

Regards
Johnny

Helllo Johnny, thanks for your answer at this time of year :slight_smile:

After I modified the routes as you’ve advised, I got a 404 - Not Found error, again in a modal and GC JS loading correctly in the network tab of the dev tools.

So I modified the routes as :

$routes->get(‘/example/customers(:any)’, ‘Example::customers’);
$routes->post(‘/example/customers(:any)’, ‘Example::customers’);

Then, I got the “Ooooops” error message, in the " Error: Not well JSON formatted response" modal :

Regards,
Laurent

Hello @ljegou ,

It seems that the function is not configured correctly. More specifically from the example below:

// Your function at your controller
public function customers()
{
    $crud = $this->_getGroceryCrudEnterprise();
    $crud->setTable('customers');
    $crud->setSubject('Customer', 'Customers');

    $output = $crud->render();

    return $this->_example_output($output);
}

private function _example_output($output = null) {
    if (isset($output->isJSONResponse) && $output->isJSONResponse) {
                header('Content-Type: application/json; charset=utf-8');
                echo $output->output;
                exit;
    }

    return view('example.php', (array)$output);
}

It seems that you are missing the lines:

if (isset($output->isJSONResponse) && $output->isJSONResponse) {
    header('Content-Type: application/json; charset=utf-8');
    echo $output->output;
    exit;
}

If this still doesn’t work, can you copy your Controller and send it here?

Regards
Johnny

Hello, thaks for your answer, the controller is the same as the tutorial. It seems i have a CI config error somewhere. I’ll search more, but the deadline is looming, so i’ll go with a simmple table for now.