Welcome Guest, Not a member yet? Register   Sign In
Routing or remap
#1

(This post was last modified: 09-30-2015, 06:58 AM by ernestlambert.)

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?
Reply
#2

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 {


Reply
#3

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?
Reply
#4

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/$
Reply
#5

try this library...

https://github.com/Patroklo/codeigniter-...vel-routes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB