Welcome Guest, Not a member yet? Register   Sign In
CI4 Routing Issue (Resolved)
#1

(This post was last modified: 08-28-2020, 04:33 AM by LynxCoder.)

Hi All,

I am coming to the line of thinking that I am missing something very simple here, but just cannot see it.

ON both my local MAMP environment on the Mac, and on a shared hosting platform, the following routing code is misbehaving.  Allow_rewrite is switched on, and the .htaccess file is working.  The code was developed initially with the alpha version of CI4, and now updated through Composer to CI4.0.4.

In /config/routes.php I have:
 * Controllers when no specific route has been defined. If false,
 * only routes that have been defined here will be available.
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
 
/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */
 
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/''Home::index');
$routes->get('/dashboard''Users::dashboard', ['as' => 'dashboard']);

That works fine, and routes to the dashboard, with no index.php file in evidence and all is ok.  

However, once the route definitions change to:
$routes->get('/', 'Home::index');
$routes->get('/dashboard/news/(:num)', 'Users::newsstory/$1');
$routes->get('/dashboard/account', 'Users::accountsettings');
$routes->get('/dashboard', 'Users::dashboard', ['as' => 'dashboard']);

Then the /dashboard route still works, but when you navigate to /dashboard/account - it says cannot find Users::account.  When you navigate to /dashboard/news/11 is says cannot find Home::index.

Anything beyond the first level routing is mis-routing.  Within the users controller are functions called dashboard, account settings and newsstory($storyid) but the latter two are never called.  I can get the segment data by breaking down the URL in the code, but that does not solve the issue of routing.

Where am I going wrong can anyone tell me?

Rich
Reply
#2

I have tried everything I can think of with this problem. I have got the URI helper to output the current URI which correctly shows no index.php in the URI, so from that I presume the .htaccess is working right.

Every time domain.com/segment1 always follows the specific routing, but when you have domain.com/segment1/segment2, it just does not follow the routing procedures. I have also tried with segment1 as a group. With the 'get' route of / within that group, it again routes fine. But put segment2 within that group, and the routing is never followed.

Could the core framework need reinstalling?
Rich
Reply
#3

Haven't tested your code, but could it be so simple that you put / in front of everything?

PHP Code:
$routes->get('/''Home::index');
$routes->get('dashboard/news/(:num)''Users::newsstory/$1');
$routes->get('dashboard/account''Users::accountsettings');
$routes->get('dashboard''Users::dashboard', ['as' => 'dashboard']); 

Does it get less confused with auto route turn off?

PHP Code:
$routes->setAutoRoute(false); 
Reply
#4

(08-27-2020, 02:45 PM)jreklund Wrote: Haven't tested your code, but could it be so simple that you put / in front of everything?

PHP Code:
$routes->get('/''Home::index');
$routes->get('dashboard/news/(:num)''Users::newsstory/$1');
$routes->get('dashboard/account''Users::accountsettings');
$routes->get('dashboard''Users::dashboard', ['as' => 'dashboard']); 

Does it get less confused with auto route turn off?

PHP Code:
$routes->setAutoRoute(false); 

Thanks. I have tried it with setAutoRoute true and its the same outcome. Also tried with and without the leading / and no difference.
Rich
Reply
#5

Problem resolved. Unfortunately, I do not know the exact answer, but it must have been a carry over somewhere from one of the pre-release versions. I started coding this site with 4.0.0-alpha, and have updated the base code with every release.

However, I have now created a fresh CI4.0.4 project, and moved all of my code into it, updated the config folder to reflect the settings I had previously, and everything now works as it should. That proves my code was ok, and it must just have been a bug that in the framework code somewhere.

All has now been committed to the GIT repo and works find on the end server as well.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB