CI Base URL Routing

For example, on Twitter you can have this URL format: http://twitter.com/username/

With the username that is the user for the user.

I am wondering how to do it correctly in Codeigniter. I would need the same format. I have other pages, such as user account management, etc. Do I need to route it through one function, check if this user exists, and then transfer it to another controller? Thank!

+3
source share
4 answers

Extend the router class by placing it MY_Router.phpin a directory application\librariesand use this code:

<?php 

class MY_Router extends CI_Router {

    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {       
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // **
        // THIS IS THE NEW CODE BELOW
        // **
        // It forces the segments to your known class (user) & method (index)
        // for all controller calls that don't exist as files or inside
        // directories

        $my_segments = array('user', 'index', $segments[0]);    

        return $my_segments;
    }
}

User , :

<?php

class User extends Controller {

    function index($username = '')
    {
        // Validate the HECK out of $username
        // Validate the HECK out of $username
        // VALIDATE THE HECK OUT OF $username
        echo $username;
        exit();
    }

}

! CI 1.7.2. 2,0, ...

+4

CI 2.0 - , :

$route['404_override'] = 'users';
+3

- :

http://twitter.com/u/username

"U"

class U extends Controller{

    function index($username){
        echo $username;
    }
}

URL-, - , , .. - , CI.

+2

, "", "", :

class Author extends CI_Controller {

public function page($username = null) {
    if($username == null) { //checking for forced url page load without username specified
        //do a 404 redirect
    } else {
        $this->load->model('m_users');
        if($this->m_users->exists($username)) { // checking if requested username exists

            //do stuff with the user here

        } else { //otherwise redirect
            //do a 404 redirect
        }
    }
}

config/routes.php " your-domain.com/author/page/username" " your-domain.com/username"

if($handle = opendir(APPPATH.'/controllers')) {
    while(false !== ($controller = readdir($handle))) {
        if($controller != '.' && $controller != '..' && strstr($controller, '.') == '.php') {
            $route[strstr($controller, '.', true)] = strstr($controller, '.', true);
            $route[strstr($controller, '.', true).'/(:any)'] = strstr($controller, '.', true).'/$1';
        }
    }
    closedir($handle);
}
$route['([a-zA-Z0-9_-]+)'] = 'author/page/$1';

your-domain.com/whatever your-domain.com/author/page/whatever, name "Whatever" . , .

, - your-domain.com/login your-domain.com/auth/login , config/routes.php

//...
$route['login'] = 'auth/login'; //add this line before the one specified above
$route['([a-zA-Z0-9_-]+)'] = 'author/page/$1';
0

Source: https://habr.com/ru/post/1755437/


All Articles