Manish Tiwari
Manish Tiwari

Reputation: 1866

How to call Controller inside sub folder using codeigniter 3.0?

I have created controller file in sub folder of controller on codeigniter 3.0.

i am using query string formate for url not segment .

i have two type of sub folder for backend(admin) and frontend(user).

i have also created MY_Router file in core folder of application folder.

Structure of Controller

Controller
--backend
   ---admin.php
   ---product.php
--frontend
   ---user.php

I want url for admin panel:

http://localhost/DemoSite/admin_panel/admin/dashboard

admin_panel want it in URL before every backend controller call

admin is Controller
dashboard is Function

For frontend :

http://localhost/DemoSite/user

I have done route like this :

$route['default_controller'] = 'frontend/user';
$route['admin_panel/(:any)'] = "backend/$1";
$route['(:any)'] = "user/$1";

MY_Router File Code:

<?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';
        }

        if (is_dir(APPPATH . 'controllers/' . $class)) {
            $this->set_directory($class);
            $class = $method;
            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }

        if (!file_exists(APPPATH . 'controllers/' . $this->directory . ucfirst($class) . '.php')) {
            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.');
    }
}

Upvotes: 0

Views: 9893

Answers (2)

user4419336
user4419336

Reputation:

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);

Upvotes: 1

Yash
Yash

Reputation: 1436

Try this:

/* for http://localhost/DemoSite/admin_panel/admin/dashboard */
$route['default_controller'] = 'frontend/user';
$route['admin_panel/(:any)/(:any)'] = "backend/$1/$2";

/* For http://localhost/DemoSite/user */
$route['(:any)'] = "frontend/$1";

Upvotes: 1

Related Questions