I want the controller placed in the subdirectory to be the default_controller.

Asked 1 years ago, Updated 1 years ago, 46 views

There is a front site and a management site, and I would like to separate the controller directory.
I would like to map the URL to the controller to be executed as shown below, but it doesn't work.
How can I resolve this?

If the directory on the front site is ffront 、 and the directory on the management site is inadmin 」

ttp://example.com/→controllers/front/Welcome
ttp://example.com/regist/→controllers/front/Regist
ttp://example.com/admin/ → controllers/admin/Welcome
ttp://example.com/admin/members/→controllers/admin/Members

The routes.php is defined as follows, but if accessed by tttp://example.com/ 」 the controller class portion becomes empty and cannot be resolved.

$route['default_controller'] = 'front/welcome';
$route ['admin/(:any)'] = 'admin/$1';
$route['admin'] = "admin/welcome";
$route['(:any)'] = "front/$1";

CI_VERSION is 3.0.3.

I would appreciate your advice.

php codeigniter

2022-09-30 21:12

1 Answers

$route['default_controller'] can only contain controller or controller/method and cannot contain directory names.CodeIgniter 2.x did this, but this behavior was unintended and was discontinued from 3.0.

Each subfolder can have a default controller that is called when only the subfolder is specified. The default controller name is the name specified in the application/config/routes.php file.
--- Quoted from the 2.x document http://codeigniter.jp/user_guide_ja/general/controllers.html#subfolders

In other words, http://example.com/front/ can be routed to front/Welcome.php by keeping $route['default_controller']="welcome";.

The only way to write a route definition in is that the current routing process does not refer to a route definition other than default_controller for access to /.Therefore, routing such as /->front/welcome requires modification to the CI_Router class.

So, the current maintainer, Andrey Andreev, left this comment.

You're looking at controller directories the long way-they are not just tools to organize your code, a request's path is supported to match a real file path.
If you consumer a route to be a "redirect", it might make sense to "redirect" a user's request, but it doesn't make sense to redirect in your index page.
--- Quoted from https://github.com/bcit-ci/CodeIgniter/issues/2849#issuecomment-37272429

In this case, if the directory structure is adjusted to the URL, will it be as follows?

  • controller/Welcome.php
  • controller/Regist.php
  • controller/admin/Welcome.php
  • controller/admin/Members.php

This makes routes.php simple.

//Applies to each directory: /->/welcome, /admin->/admin/welcome
$route['default_controller'] = 'welcome';

// $route ['admin/(:any)'] = 'admin/$1'; also not required


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.