• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove controller name from the URL

#1
Smile 
Hi, i would like remove the controller name from the URL. I have tried with the routes but i failed.
I have this URL: domain.com/main/privacy and i would like transform it in: domain.com/privacy

Thanks for help me! Smile
Reply

#2
Hi, you type your route like this:

PHP Code:
$routes->add('privacy''Main::privacy'); 

If you only wan't the method privacy to work.
Reply

#3
Hi! Thanks for your answer. I tried your code but doesn't work  Undecided
In codeigniter 3 i used this routes for remove the controller name from the URL:

PHP Code:
$route['default_controller'] = 'main';
$route['(:any)'] = "main/$1"

In codeigniter 4 what routes i have to use for remove MAIN (the controller name) from the URL? This is my Routes.php file.

PHP Code:
/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Main');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(true);
$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('/', 'Main::index');
$routes->add('privacy''Main::privacy'); 

Thanks  Smile
Reply

#4
Try setting $indexPage='' (in \app\Config\App.php).
Reply

#5
(04-21-2020, 06:47 AM)Gary Wrote: Try setting $indexPage='' (in \app\Config\App.php).

I'm sorry, your code doesn't work  Sad
Reply

#6
To redirect everything you need an any rule.
PHP Code:
$routes->add('(:any)''Main::$1'); 

And the previous provided routes work just fine. You most likely made an error in your Main.php file.
Reply

#7
Now is all ok! Thanks.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.