Welcome Guest, Not a member yet? Register   Sign In
Auto routing and/or defining routes
#1

Hello community
I'm struggling with routing in my new application. I'm coming from CI3 and am used the auto routing stuff. Now I learned about defining routes for the application. But I can't find the right solution that works for my purposes.
What I like to achieve is to route a handful of URIs to some specific controllers, some of them organised in a sub directory. Then I have all the other URIs which I'd like to route to a single controller to get the dynamic page content together.
Controllers' directory:
-- BaseController.php
-- Site.php
-- Login.php
-- Apps
-- -- Home.php
-- -- App1.php
-- -- App2.php
-- -- …
The Site controller should get every request from URIs not beginning with login or apps/appX. So I defined a route like this:
PHP Code:
$routes->get('/''Site::index');
$routes->get('/login''Login::index');
$routes->get('/(:segment)''Site::index');
$routes->get('/(:segment)/(:any)''Site::page'); 

What happens now is that all the dynamic URIs are routed to Site::index and Site::page. So far so good. The Login page also works fine. For the AppX controllers I wanted to use auto routing, because there are a lot of controllers inside the App sub directory. So I activated auto routing as mentioned in the user guide.
But I can't get it working. If I call /apps/app1 I get routed to Site::page. I guess the problem is that CI is working the defined routes before looking for a specific controller file for auto routing to it.
My questions:
Is it generally not possible to define routes when auto routing is enabled?
How can I achieve what I'm trying to?
Thanks for your help!
Reply
#2

Yes, the following defined routes catch all URIs.

PHP Code:
$routes->get('/(:segment)''Site::index');
$routes->get('/(:segment)/(:any)''Site::page'); 

How did you do with CI3?
Reply
#3

(This post was last modified: 09-24-2024, 12:13 AM by bastian.)

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

(09-23-2024, 11:40 PM)bastian Wrote: 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?

You misunderstand CI routing.
It checks defined routes first, and if not found, find the controller by auto routing (if it is enabled).
Reply
#5

You have a typo in the namespace.


Code:
namespace App\Controller\Apps;  <-- wrong
namespace App\Controllers\Apps; <-- correct
Reply
#6

(09-24-2024, 02:02 AM)kenjis Wrote: You misunderstand CI routing.
It checks defined routes first, and if not found, find the controller by auto routing (if it is enabled).

Ah, I see. Thank you for clarifying it.


Oh and the typo is just so annoying. Okay, so now I found out, that I have to prefix methods of controllers in the sub folder with "get". And redirecting isn't working anymore in the constructor. Oh holy… that transformation is really tough!


I guess I'll go for defining all the routes and letting auto routing disabled. Seems the safer way for me.


Thank you for your help, kenjis!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB