Routing Controllers in Subfolders Using Codeigniter

Routing controllers in subfolders using CodeIgniter

Do this:

$route['store/(:any)'] = 'frontend/store/$1';
$route['processing/(:any)'] = 'frontend/processing/$1';
$route['profile/(:any)'] = 'frontend/profile/$1';

Same for backend :

$route['backend/(:any)'] = 'backend/authenticate/$1';

You don't have to create each rule in routes.php for every function of the controller, rather one rule per controller will be enough as mentioned above.

URI Routing : CodeIgniter User Guide

$1 represent the first expression, here (:any) is the expression, you can have multiple expression on each rule, and expression is represented as $1, $2 and so on on the other side.

Similarly, (:num) will match a segment containing only numbers, (:any) will match a segment containing any character, (\d+) will match any digit, ([a-z]+) will match any alpha text.

How to routing controllers in sub folders using codeigniter 3?

By default codeIgniter 3 versions and up you can not use sub folders in your default controller route.

To be able to use a sub folder in default_controller you need to use a MY_Router.php

$route['default_controller'] = 'admin/login';

application >

application > core >

application > core > MY_Router.php

MY_Router.php

<?php

class MY_Router extends CI_Router {
protected function _set_default_controller() {

if (empty($this->default_controller)) {

show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}

// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {

// Set the class as the directory

$this->set_directory($class);

// $method is the class

$class = $method;

// Re check for slash if method has been set

if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}

if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {

// This will trigger 404 later

return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}

Also make sure your you follow the codeIgniter way of naming files

File name: Login.php

<?php 

class Login extends CI_Controller {
public function index() {

}
}

Routing controllers in sub folders - Codeigniter 3

I am maintaining my admin related controller's in a directory (Controllers/admin/)

//ADMIN

//$route['folder'] = "folder/home";
$route['admin'] = 'admin/admin';

$route['admin/dashboard'] = 'admin/dashboard';
$route['admin/logout'] = 'admin/profile';
$route['admin/home'] = 'admin/home';

How to call Controller inside sub folder using codeigniter 3.0?

You say you are using query strings. When using query strings.

Change this

$config['uri_protocol'] = 'REQUEST_URI';

To this

$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'QUERY_STRING';

Then enable

$config['enable_query_strings'] = TRUE;
// Controller
$config['controller_trigger'] = 'c';
// Function
$config['function_trigger'] = 'm';
// Directory
$config['directory_trigger'] = 'd';

As shown on user guide

http://localhost/your_project/index.php?d=admin_panel&c=admin&m=dashboard

Dashboard would be a function on your admin controller for example.

how to use a site url with query string

site_url('d=admin_panel&c=admin&m=dashboard');

Width user id example

$id = '1';
site_url('d=admin_panel&c=admin&m=dashboard&user_id=' . $id);

How to work with subdirectory controllers in CodeIgniter 4?

As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.

namespace App\Controllers\Admin;
use App\Controllers\BaseController;

class Dashboard extends BaseController
{
public function index()
{

}
}

Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.



Related Topics



Leave a reply



Submit