Welcome Guest, Not a member yet? Register   Sign In
Auto Routing and redirect() not working
#1

(This post was last modified: 01-30-2023, 12:45 AM by objecttothis.)

Converting an application from CI3.x to CI 4.1.3

My Controllers are in app/Controllers/

PHP Code:
<?php

namespace App\Controllers;

use 
App\Libraries\MY_Migration;
use 
App\Models\Employee;
use 
Config\Migrations;
use 
Config\Services;

/**
 * @property employee employee
 */
class Login extends BaseController


My app/Config/Routes.php includes
PHP Code:
<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH 'Config/Routes.php'))
{
 require 
SYSTEMPATH 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Login');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true); 

but php spark routes includes no "auto" entries and I end up with an error when I navigate to https://[domain]/pos_dev/public


CodeIgniter\HTTP\Exceptions\HTTPException

route cannot be found while reverse-routing.
Reply
#2

Please Read:
CodeIgniter 4 User Guide - Controllers and Routing - Controllers - Auto Routing (Legacy)

It is recommend that you not use Auto Routing (Legacy).
You are using an old version of CodeIgniter 4 and should upgrade to the new version 4.2.10
and use the New Improved Auto Routing.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

It's been a crazy undertaking for me to convert this CI3.x app to 4.1.3, so I was trying to get through that before upgrading then to 4.2.10. Improved Auto Routing however convinced me, so I updated CI4 to 4.2.10 and enabled Improved Auto Routing per the user guide (https://codeigniter.com/user_guide/incom...g-improved).

app/Config/Routes.php
PHP Code:
$routes->setAutoRoute(true); 

app/Config/Feature.php
PHP Code:
public bool $autoRoutesImproved true

app/Controllers/Home.php is setup
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function 
index()
    {
        return 
view('welcome_message');
    }


So I don't understand why even the improved auto routing is not picking up the controller.

when I navigate to the public folder I get a slightly different error now: CodeIgniter\HTTP\Exceptions\HTTPException The route for "home" cannot be found. SYSTEMPATH/HTTP/RedirectResponse.php at line 60

The end of the index() function in my Login controller has
PHP Code:
return redirect('home'); 
and I do have some routes manually specified.

PHP Code:
$routes->get('/''Login::index');
$routes->add('no_access/([^/]+)''No_access::index/$1');
$routes->add('no_access/([^/]+)/([^/]+)''No_access::index/$1/$2');

$routes->add('sales/index/([^/]+)''Sales::manage/$1');
$routes->add('sales/index/([^/]+)/([^/]+)''Sales::manage/$1/$2');
$routes->add('sales/index/([^/]+)/([^/]+)/([^/]+)''Sales::manage/$1/$2/$3');

$routes->add('reports/(summary_:any)/([^/]+)/([^/]+)''Reports::summary_(.+)/$1/$2/$3/$4'); //TODO - double check all TODOs
$routes->add('reports/summary_expenses_categories''Reports::date_input_only');
$routes->add('reports/summary_payments''Reports::date_input_only');
$routes->add('reports/summary_discounts''Reports::summary_discounts_input');
$routes->add('reports/summary_:any''Reports::date_input');

$routes->add('reports/(graphical_:any)/([^/]+)/([^/]+)''Reports::/$1/$2/$3/$4'); //TODO
$routes->add('reports/graphical_summary_expenses_categories''Reports::date_input_only');
$routes->add('reports/graphical_summary_discounts''Reports::summary_discounts_input');
$routes->add('reports/graphical_:any''Reports::date_input');

$routes->add('reports/(inventory_:any)/([^/]+)''Reports::/$1/$2'); //TODO
$routes->add('reports/inventory_summary''Reports::inventory_summary_input');
$routes->add('reports/(inventory_summary)/([^/]+)/([^/]+)/([^/]+)''Reports::/$1/$2'); //TODO

$routes->add('reports/(detailed_:any)/([^/]+)/([^/]+)/([^/]+)''Reports::/$1/$2/$3/$4'); //TODO
$routes->add('reports/detailed_sales''Reports::date_input_sales');
$routes->add('reports/detailed_receivings''Reports::date_input_recv');

$routes->add('reports/(specific_:any)/([^/]+)/([^/]+)/([^/]+)''Reports::/$1/$2/$3/$4'); //TODO
$routes->add('reports/specific_customer''Reports::specific_customer_input');
$routes->add('reports/specific_employee''Reports::specific_employee_input');
$routes->add('reports/specific_discount''Reports::specific_discount_input');
$routes->add('reports/specific_supplier''Reports::specific_supplier_input');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH 'Config/' ENVIRONMENT '/Routes.php')) {
    require 
APPPATH 'Config/' ENVIRONMENT '/Routes.php';

Reply
#4

Auto Routing (Legacy) should work with your first config.
I don't know why it does not work.

Auto Routing (Improved) is much complex than  Legacy for the security reasons.
Read this carefully:
https://codeigniter4.github.io/CodeIgnit...to-routing
Reply
#5

(This post was last modified: 01-30-2023, 12:54 AM by objecttothis.)

(12-10-2022, 05:58 PM)kenjis Wrote: Auto Routing (Legacy) should work with your first config.
I don't know why it does not work.

Auto Routing (Improved) is much complex than  Legacy for the security reasons.
Read this carefully:
https://codeigniter4.github.io/CodeIgnit...to-routing

See below.  The issue is now redirect().

After returning from vacation I upgraded the core framework to 4.3.1 then walked through all the upgrade path steps. Still no dice, so to troubleshoot I commented out all my defined routes except for the root directory $routes->get('/', 'Login::getIndex');.  Then I ran php spark routes again and the  autorouting began working. So, I figured that it was one of my defined routes breaking auto routing of all controllers. I uncommented each defined route individually down to uncommenting all of them but autorouting remained working. Oh well.

After all that I'm still getting "The route for "testRoute" cannot be found." I can't seem to attach images, so I'll have to write this all in text.

In Routes.php I have:
PHP Code:
$routes->get('/''Login::getIndex'); 

That is routing properly. At the end of the getIndex function in the Login controller I have:
PHP Code:
return redirect('testRoute'); 

Under app\Controllers\ I have TestRoute.php
PHP Code:
<?php

namespace App\Controllers;
class 
TestRoute extends BaseController
{
 public function 
getIndex()
 {
 echo 
"hello world";
 }


When I navigate the browser to /public/ it loads getIndex() from Login.php but when it gets to the last line it kicks out:
PHP Code:
The route for "testRoute" cannot be found

It doesn't seem to matter what I change the controller to in the redirect() function.

php spark routes clearly shows:
PHP Code:
GET(auto)    testRoute                                          |      | \App\Controllers\TestRoute::getIndex                  honeypot csrf invalidchars honeypot secureheaders toolbar 

It's unclear to me why redirect isn't working. Can redirect() no longer be used with auto routing like it could be in CI3? If I navigate to /public/testRoute/ it displays "hello world" as it should.
Reply
#6

redirect() is completely different from redirect() in CI3.

See https://codeigniter4.github.io/CodeIgnit...l#redirect

If you write redirect('testRoute'), the testRoute is a route name, not URI path.
A route name is a name for a defined route.

Try:
PHP Code:
return redirect()->to('testRoute'); 
Reply
#7

(This post was last modified: 01-30-2023, 01:11 AM by objecttothis. Edit Reason: clarity )

OK,
PHP Code:
return redirect()->to('testRoute'); 
This works.  IMO the documentation (https://codeigniter.com/user_guide/gener...l#redirect) isn't clear that redirect()->to('controllerName') is how to redirect to a route when using auto routing. Also, in the Upgrading from 3.x to 4.x guide (https://codeigniter.com/user_guide/insta...ml#helpers) you do mention that redirect now needs to be returned but it would be helpful to mention that if using auto routing
PHP Code:
redirect('home'); 
becomes
PHP Code:
return redirect()->to('home'); 

Thanks very much for your help here!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB