0

I am doing a project in CodeIgniter and I want to route all the urls of a particular controller to a specific action except one. For e.g., I want the url

myurl/mycontroller/myaction

to be handled by the action myaction but any other urls like

myurl/mycontroller/myaction1
myurl/mycontroller/myaction2
myurl/mycontroller/myaction3

to be handled by action abc of a particular controller. I had searched across the internet and what I get is how to handle all urls by a certain controller except some. The way to do it is

$route['^(?!admin|user|setup|pages).*'] = "user/view/$0";

Here all urls will be handled by user/view except those whose 2'nd part of the url is admin, user, setup or pages.

Suman
  • 49
  • 1
  • 5

2 Answers2

0

I think routes are applied in order, so how about adding a route for the "myaction" first before the other ones?

$route['myurl/mycontroller/myaction'] = "myurl/mycontroller/myaction";
$route['myurl/mycontroller/abc'] = "myurl/mycontroller/$1";
sdot257
  • 10,046
  • 26
  • 88
  • 122
  • I think you got me wrong. I had asked how any action name can be handled by a particular action of a controller except one. E.g. `myurl/mycontroller/myaction1` will be handled by `myurl/controller_a/action` with `myaction1` as an argument. Similarly, `myurl/mycontroller/myaction2` will also be handled by `myurl/controller_a/action` with `myaction2` as an argument. – Suman Nov 04 '11 at 14:22
0

I believe this is the correct syntax

$route['myurl/mycontroler/myaction(:any)'] = "myurl/controller_a/action";

You can verify it here

EDIT

I read your comment and I made an adjustment. Give it a go and see if it fits.

EDIT 2

Well since you just want the exact word myaction unharmed then either use (:any) or (\d+) after the word so the rerouteing happens when a number is attached to the myaction word. I haven't actually tested it yet.

Sotiris K.
  • 88
  • 7
  • I checked that. What happens now is `myurl/mycontroller/myaction1` gets handled by `myurl/controller_a/action` with `myaction1` as argument, but even `myurl/mycontroller/myaction` gets handled by `myurl/controller_a/action` with `myaction` as an argument! – Suman Nov 04 '11 at 14:45
  • 1
    And what if the work `myaction` is dynamic? I give you the situation that I am stuck with.I have an url `http://mysite/artists/view/A` This is handled by `view` action of `artists` controller. Now I want artists profile at `http://mysite/artists/brittney` or `http://mysite/artists/michael` or `http://mysite/artists/shakira` I have designed an action `function viewartist($alias)` in my `artists` controller. How do I get the alias in this action? – Suman Nov 04 '11 at 15:12
  • @sumannath Inside the main function of your controller where the parent::__construct takes place you should load the appropriate view or the appropriate function based on the URI segment. I'm sorry but next time please be more specific. I believe you should really read about the routing from the link in my post. – Sotiris K. Nov 04 '11 at 15:19
  • Ok. I'll keep that in mind. This is my first post. I hope you will pardon me this time. About the answer, I have that in mind as the last way. Is there anyway it can be done via routes.php? – Suman Nov 04 '11 at 15:24