user4419336
user4419336

Reputation:

Custom MY_Router modification in codeigniter 3.0.6

When I use this url

index.php?route=account/edit

controller > account > Account.php < -- Edit function on controller

The code above works with my custom MY_Router.php

How ever when I try and access another controller

index.php?route=account/register it does not work

controllers > account > Register.php

On my MY_Router.php I have a variable which is $part[1] and gets the second segment.

How ever I need to be able to make sure it can check if its a controller or function

Question: How can I make sure when I use set_class and set_method that it can detect if its a controller or function?

<?php

class MY_Router extends CI_Router {

    protected function _set_routing() {

        if (file_exists(APPPATH.'config/routes.php'))
        {
            include(APPPATH.'config/routes.php');
        }

        if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
        {
            include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
        }

        // Validate & get reserved routes
        if (isset($route) && is_array($route))
        {
            isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
            isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
            unset($route['default_controller'], $route['translate_uri_dashes']);
            $this->routes = $route;
        }

        if ($this->enable_query_strings) {

            if ( ! isset($this->directory))
            {
                $route = isset($_GET['route']) ? trim($_GET['route'], " \t\n\r\0\x0B/") : '';

                if ($route !== '')
                {
                    $part = explode('/', $route);

                    if ( ! empty($part[1])) {

                        $this->uri->filter_uri($part[0]);
                        $this->set_directory($part[0]);
                        $this->set_class($part[0]);

                        // Testing only
                        if ( ! empty($part[1]))
                        {
                            $this->uri->filter_uri($part[1]);
                            $this->set_method($part[1]);
                        }

                        $this->uri->rsegments = array(
                            1 => $this->class,
                            2 => $this->method
                        );
                    }

                } else {

                    $this->_set_default_controller();
                }
            }

            // Routing rules don't apply to query strings and we don't need to detect
            // directories, so we're done here
            return;
        }

        // Is there anything to parse?
        if ($this->uri->uri_string !== '')
        {
            $this->_parse_routes();
        }
        else
        {
            $this->_set_default_controller();
        }
    }
}

Upvotes: 0

Views: 904

Answers (2)

user4419336
user4419336

Reputation:

Thanks to @vijaykumar was able to get it to work.

<?php

class MY_Router extends CI_Router {

    protected function _set_routing() {

        if (file_exists(APPPATH.'config/routes.php'))
        {
            include(APPPATH.'config/routes.php');
        }

        if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
        {
            include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
        }

        // Validate & get reserved routes
        if (isset($route) && is_array($route))
        {
            isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
            isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
            unset($route['default_controller'], $route['translate_uri_dashes']);
            $this->routes = $route;
        }

        if ($this->enable_query_strings) {

            if ( ! isset($this->directory))
            {

                $_route = $this->config->item('route_trigger');
                $_route = isset($_GET[$_route]) ? trim($_GET[$_route], " \t\n\r\0\x0B/") : '';

                if ($_route !== '')
                {
                    $part = explode('/', $_route);

                    if ( ! empty($part[1])) {

                        if (file_exists(APPPATH . 'controllers/' . $part[0] . '/' . ucfirst($part[1]) . '.php')) {

                            $this->uri->filter_uri($part[0]);
                            $this->set_directory($part[0]);

                            $this->uri->filter_uri($part[1]);
                            $this->set_class($part[1]);

                            $_f = trim($this->config->item('function_trigger'));

                            if ( ! empty($_GET[$_f])) {
                                $this->uri->filter_uri($_GET[$_f]);
                                $this->set_method($_GET[$_f]);
                            }

                            $this->uri->rsegments = array(
                                1 => $this->class,
                                2 => $this->method
                            );

                        } else {

                            $this->uri->filter_uri($part[0]);
                            $this->set_directory($part[0]);
                            $this->set_class($part[0]);

                            $this->uri->filter_uri($part[1]);
                            $this->set_method($part[1]);    

                            $this->uri->rsegments = array(
                                1 => $this->class,
                                2 => $this->method
                            );

                        }
                    }

                } else {

                    $this->_set_default_controller();
                }
            }

            // Routing rules don't apply to query strings and we don't need to detect
            // directories, so we're done here
            return;
        }

        // Is there anything to parse?
        if ($this->uri->uri_string !== '')
        {
            $this->_parse_routes();
        }
        else
        {
            $this->_set_default_controller();
        }
    }
}

Upvotes: 0

vijaykumar
vijaykumar

Reputation: 4806

I'm not sure. Here is my idea

Add on check before $this->set_class($part[0]);

if(file_exists(APPPATH."controllers/$part[0]/$part[1].php")){
   //Second parameter has controller class load
}
else{
  // controller doesn't exist
}

Upvotes: 1

Related Questions