How to direct traffic in an MVC application: CodeIgniter $ routes or __remap () to select a controller?

Reference Information:

I use the HMVC plugin for CodeIgniter, but this question is most likely for MVC in general.

Question:

Do I have to use $routesURLs to map the layout to call specific controllers / modules, or do I need to create functions __remap()in each of my controllers to call the next "module" in a series of URLs?

I can implement any of these methods successfully, but I really don't see a clear winner. I am looking for a reason to use one method over another (if a combination of both does not work).

Also, I am not looking for any help on the syntax, so if there are small errors below, you can ignore them, I am just looking for how to apply the best MVC methods (HMVC) on my site.

Possible URLs for visitors:

Assuming the following URLs may appear in my application:

Main Site:
http://www.example.com

Individual Static Pages:
                   .../staticPage1
                   .../staticPage2

Profession Sections:
                   .../profession1
                   .../profession2

Users Section (Unique to each profession):
                   .../profession#/users
                               .../users/id
                               .../users/dashboard
                               .../users/edit
                               .../users/preferences

Events Section (Unique to each profession):
                   .../profession#/events
                               .../events/id
                               .../events/dates
                               .../events/location
                               .../events/create

Search Page (Unique to each profession):
                   .../profession#/search

Controllers matching the above URL segments:

and related controllers:

  • Site
  • Pages
  • Professions (extended as special professions #).
  • Users
  • Events
  • Search

CodeIgniter directory structure for my controllers:

which act as their own modules and are saved as such:

/applications
             /modules
                     /site/
                     /pages/
                     /professions/
                     /users/
                     /events/
                     /search/

Each module has a subdirectory models, viewsand controllers.

Example $ routes:

If I used a router to achieve all this, I would use the following routes, which I defined in the file config:

//Defaults
$route['default_controller'] = 'site';
$route['404_override'] = '';

//Users
$route['(?i)([a-z0-9_-]+)/users'] = 'users';
$route['(?i)([a-z0-9_-]+)/users/(:num)(/(:any))?'] = 'users/dashboard/$2';
$route['(?i)([a-z0-9_-]+)/users/(:any)'] = 'users/$2';

//Specific Sections
$route['(?i)([a-z0-9_-]+)/search'] = 'search/eventSearch';

//Events
$route['(?i)([a-z0-9_-]+)/events'] = 'events';
$route['(?i)([a-z0-9_-]+)/events/(:num)(/(:any))?'] = 'events/getById/$2';
$route['(?i)([a-z0-9_-]+)/events/(:any)'] = 'events/getByCategory/$2';

//Profession1
$route['(?i)profession1'] = 'profession1';
$route['(?i)profession1(/(:any)?)'] = 'profession1/$2';


//Profession2
$route['(?i)profession2'] = 'profession2';
$route['(?i)profession2(/(:any)?)'] = 'profession2/$2';

//Pages
$route['(?i)(:any)'] = "pages/index/$1";

__remap()

__remap(), , , . Professions:

public function _remap( $method )
{
        if( isset( $this->uri->segment(3) ) )
        {
            $method2 = $this->uri->segment(3);
        } else {
            $method2 = 'index';
        }
    if ($method == 'users')
    {
        $method2 = $this->uri->segment(3);
        $this->load->module('users');
        $this->users->$method2();
    }
    elseif ($method == 'events')
    {
        $method2 = $this->uri->segment(3);
        $this->load->module('events');
        $this->events->$method2();
    }
    elseif ($method == 'search')
    {
        $this->load->module('search');
        $this->search->$method2();
    }
    else
    {
        $this->$method();
    }
}

:

- , , , .

+3
1

$route remap , . CodeIgniter, .

. , URL, , .

_remap() , ( ) ( 2- uri). .
_remap , .

public function _remap( $method )
{
    $function = array(  'users'     =>  'users',
                        'events'    =>  'courses',
                        'search'    =>  'search');
    switch ($method) {
        case 'users':
        case 'events':
        case 'search':
            $method2 = ($this->uri->segment(3) ?: 'index');
            $this->load->module($method);
            $this->$function[$method]->$method2();

            break;
        default:
            $this->$method();

            break;
    }
}

, , , , _remap() $route . $route, .

+1

All Articles