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.');
    }
}
Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65
  • 2
    same question you had asked yesterday.. see: http://stackoverflow.com/questions/35627730/how-to-routes-controller-sub-folder-using-codeigniter#comment58937353_35627730 – Pathik Vejani Feb 26 '16 at 07:12
  • This will help you http://stackoverflow.com/questions/6529026/codeigniter-default-controller-in-a-sub-directory-not-working – Gopalakrishnan Feb 26 '16 at 07:12
  • check this http://stackoverflow.com/questions/12443275/how-to-call-sub-folder-controller-with-routing-codeigniter?rq=1 – naseeba c Feb 26 '16 at 07:14
  • I might pay to read more of http://www.codeigniter.com/user_guide/general/routing.html –  Feb 26 '16 at 07:16
  • If your using query string read http://www.codeigniter.com/user_guide/general/urls.html?highlight=query%20string#enabling-query-strings and change your uri_protocol to QUERY on config.php –  Feb 26 '16 at 07:20
  • @Gopal this is for codeIgniter 2 that question. –  Feb 26 '16 at 11:18

2 Answers2

1

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";
Yash
  • 1,446
  • 1
  • 13
  • 26
1

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);
  • Thanks to give answer, I have one issue how to redirect to url when function call from controller. Right now i have load view and url is same as before but i want to change url or controller name like when first url is c=admin_controller&m=index&d=admin after redirect to c=admin_controller&m=dashboard&d=admin – Manish Tiwari Feb 26 '16 at 10:20