CodeIgniter Forums
Routing or remap - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Routing or remap (/showthread.php?tid=63144)



Routing or remap - ernestlambert - 09-30-2015

I have the controller "profiles" and I want the URI "/profile/1" to map to "/profiles/view/1" also "/property/1" should map to "/properties/view/1. I want to do this for all my controllers so is there a way of writing a general rules in the routes file or via remap so that the singular of the class name followed by a slash and a number routes to the class name followed by "/view/" and the number?


RE: Routing or remap - Martin7483 - 09-30-2015

You can setup routes, or you can use a MY_Controller with a _remap function and extend your controller from your MY_Controller.

Create MY_Controller.php in ./application/core

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller {
 
 
   public function __construct() {
 
       log_message('debug'"Class MY_Controller Initialized.");
 
 
       parent::__construct();
 
   }
 
 
   public function _remap($method) {
 
       // Check if the method is available
 
       ifmethod_exists($this$method) ) {
 
           $this->{$method}();
 
       } else {
 
           // Method is your arg value for the view method
 
           $this->view($method);
 
       }
 
   }



Now to use this just extend your controllers from MY_Controller instead of CI_Controller

PHP Code:
class Your_Controller extends MY_Controller {





RE: Routing or remap - ernestlambert - 09-30-2015

Thank you for your response.

The problem I experience now is that the controller "profile" does not exist, it is actually named "profiles". As such, the url "/profile/1" gives me a 404 page not found message but the url "/profiles/1" can then be handled by the _remap function...Any ideas?


RE: Routing or remap - Martin7483 - 09-30-2015

Ah, I misread that in your original post. Then you should set it with routes.

Add routes to the routes.php file in ./application/config
PHP Code:
/* Route a url with profile as the first segment to the method view in the
 * controller profiles passing in the match as a variable to the method.
 * (:any) will match anything in the second segment
 * use (:num) to only match numbers
 */
$route['profile/(:any)'] = profiles/view/$1
$route
['property/(:any)'] = properties/view/$



RE: Routing or remap - ngangchill - 09-30-2015

try this library...

https://github.com/Patroklo/codeigniter-static-laravel-routes