0

I want both these urls:

/admin/users/add

and

/admin/users/3/edit

to point to edit($user_id = 0) function in my users controller. The number 3 in the second url has to be passed to the $user_id parameter.

How can I do this in a smooth way?

Mischa
  • 42,876
  • 8
  • 99
  • 111
Peter Westerlund
  • 741
  • 1
  • 10
  • 36

4 Answers4

3

By setting up a route in application/config/routes.php:

$route['admin/users/add'] = "users/edit";
$route['admin/users/(:num)/edit'] = "users/edit/$1";

If you want this to work for other controller too, you can do this:

$route['admin/(:any)/add'] = "$1/edit";
$route['admin/(:any)/(:num)/edit'] = "$1/edit/$2";

Or the same, using regular expressions:

$route['admin/([a-z]+)/add'] = "$1/edit";
$route['admin/([a-z]+)/(\d+)/edit'] = "$1/edit/$2";
Mischa
  • 42,876
  • 8
  • 99
  • 111
1

As an alternative to separate your logic.

I generally have two controllers that both speak to the same view.

admin/user/add
admin/user/edit/3

Both point to the view

admin/user_form.php

Which then access a save_user() method when the form has been posted.

But as Mischa said, by setting up routes you can point pretty much any url to any method.

hoppipolla
  • 235
  • 1
  • 7
  • Yes, but I don't like that you have to set up new routes for every controller. If you could set up routes within your controllers, I would use that. – Peter Westerlund Sep 13 '12 at 07:44
  • @PeterWesterlund, you don't have to set up a route for every controller. Using a regex, you can set up a single route that works for all controllers. Besides you didn't mention in your question that the solution should work for other controllers too. If this is important mention it in your question next time. – Mischa Sep 13 '12 at 13:46
0

Can you do this

public function users ($type, $id = null)
{
     if ($type === 'edit')
     {
           // do edit stuff
     }
     else
     {
           // ad add stuff
     }
 }
Laurence
  • 58,936
  • 21
  • 171
  • 212
-3

Sulotion:

function _remap($method)
{
$param_offset = 2;

// No method, point to...
if (!method_exists($this, $method))
{
    if (is_numeric($method) || $method == 'add')
    {
        // Show single
        $param_offset = 1;
        $method = 'show';
    }
    else
    {
        // Index
        $param_offset = 1;
        $method = 'index';
    }
}

// Since all we get is $method, load up everything else in the URI
$params = array_slice($this->uri->rsegment_array(), $param_offset);

// Call the determined method with all params
call_user_func_array(array($this, $method), $params);
}
Peter Westerlund
  • 741
  • 1
  • 10
  • 36
  • @Mischa: I don't like CI's routes. It seems that you have to make new routes for all controller classes. If I use routes it should work for all classes. Otherwise, I would make the route settings directly in the class. – Peter Westerlund Sep 14 '12 at 06:46
  • You don't have to set up routes for all controllers. You can use a regex to dynamically map a route to the required controller. That way you can use one route for all controllers with the same pattern. Two lines of code is more "smooth" than what you're doing above, in my opinion at least. – Mischa Sep 14 '12 at 06:54
  • Please mention in your question that you want this to work not just for users, but for other controllers too. This is important information and people can take it into consideration when answering. I took the time to answer your question, but you didn't even considered my answer because "you don't like CI routes" and your ignorance of not knowing that a single route can be used for more than one controller. If you had included that vital information I would have shown you how to do it with routes. – Mischa Sep 14 '12 at 07:45
  • @Mischa: First of all, I'm sorry for any misunderstanding! I'm new to CodeIgniter and it's possible that I misunderstood something in your answers because of the language as well (I'm a non-english speaking). And what I actually meant when I said I didn't like the routes, is the fact that it's an separate file. If I could determine the routes directly from the Classes, that would be the absolute best. Again, I'm sorry I didn't explain this. – Peter Westerlund Sep 16 '12 at 22:15
  • So you will add a `_remap` function to all your controllers? Don't you think it's better to have all the routes in one central place instead of scattered in unclear code all over your controllers? To get the most out of a framework you should learn to use it how it was intended (by using routes). Well, that's my opinion at least. Good luck. – Mischa Sep 16 '12 at 23:23