In CI3 I used auto routing only. I didn't had a route for the dynamic first uri segment, instead I created a controller for every possible first segment (around 30 files). I thought with defining routes I could reduce those controllers and route every request to a single controller instead (Site::index and Site::page).
What do you think about me mixing defined routes with auto routing? Is that generally a problem? I thought CI would check for an existing controller in a given directory and – if that does not exist – then reads the matching route defined in Routes.php. Am I making a mistake in my thinking here?
I'm trying a new approach. As you, kenjis, mentioned, these routes seem to be a problem:
PHP Code:
$routes->get('(:segment)', 'Site::index');
$routes->get('(:segment)/(:any)', 'Site::page');
I'm going for defining routes for every 30+ possible first uri segments. The possible segments are stored in a table. So I'm going like this now in Routes.php:
PHP Code:
$this->db = \Config\Database::connect();
$query = $this->db->table('places')->get();
foreach ($query->getResult() as $place) {
$routes->get($place->uri, 'Site::index');
$routes->get($place->uri.'/(:any)', 'Site::page');
}
With that I'll be able to dynamically add new places and getting the corresponding routes automatically.
Still not solved is the problem with controllers inside the App sub directory. With auto routing still enabled, I think those controllers should get addressed with an uri like that:
/apps => routing to /Apps/Home.php
/apps/app1 => routing to /Apps/App1.php and the index method
/apps/app1/settings => routing to /Apps/App1.php and the settings method
But I get the following error:
Code:
404 - No controller is found for: apps/app1
The controller App1.php is located in Controllers/Apps/ with the following code:
PHP Code:
<?php
namespace App\Controller\Apps;
use App\Controllers\BaseController;
class App1 extends BaseController
{
public function index()
{
…
}
}
I don't understand where the problem is.