CakePHP controller alias
Asked Answered
P

4

5

I know there are a couple of other topics about this subject, but non of them seems to fit my needs.

What I have

  • example.com/log/
  • LogsController.php

I have LogsController instead of LogController (plural) because CakePHP wants you to have controllers in plural.

But as you might know/notice, example.com/log/ will never use LogsController because of the missing 's' in the url.

Now, I want to have /log/* being redirected to /logs/*. Works perfectly fine with the following code:

Router::connect ('/log/*', array('controller'=>'logs'));

But, when I try to access example.com/log/actions/foo/bar it doesn't seem to work. So after some Googeling I found this:

Router::connect ('/log/:action/*', array('controller'=>'logs'));

Works great. But now when I'm trying to access example.com/log/ again, it says

Error: LogController could not be found.

Question So my question is, how do I set up an alias for my url so that /log/ will use LogsController instead of trying to use LogController.

I have a few more Controllers where I'd like to change this, like flight => FlightsController, profile => ProfilesController.


Have a look at this question. It is about the same subject, but slightly different. It might help you in some way.

Podgorica answered 12/8, 2012 at 20:18 Comment(2)
Read this #9959652Manganite
It's easier to just tell cake that "flight" is both singular and plural.Manganite
P
10

Ok, with the help of some other people on IRC and stuff like that. I found out the following.

A combination of

Router::connect('/flight/:action/*', array('controller'=>'flights'));
Router::connect('/flight/*', array('controller'=>'flights'));

does the trick. I tried this before, but in a other order, like so:

Router::connect('/flight/*', array('controller'=>'flights'));
Router::connect('/flight/:action/*', array('controller'=>'flights'));

which doesn't work.

So the first 2 lines of code in this post solved it for me. Another guy told me that the solution of Arun Jain isn't a proper solution, because it changes the nameconventions in the core as well. Which will cause problems with the FormsHelper and classes like that. So I think I will prefer the code in this post since this is just an alias instead of a core changing piece of script. Thanks for the help anyway.

Podgorica answered 13/8, 2012 at 14:30 Comment(0)
M
1

To do this with routing the correct approach is as follows.

Router::connect('/flight', array('controller'=>'flights','action'=>'index'));
Router::connect('/flight/:action/*', array('controller'=>'flights'));

This tells the router that when an action is found in the URL to use it, but it no params are found then to default is to use the index action.

Manganite answered 13/8, 2012 at 14:52 Comment(0)
C
0

I have a slightly different take on all of this. The plural is more often the more accurate way to go with things but in occassions when the plural is just plain wrong, i add an exception into the Inflector class (/lib/Cake/Utility/Inflector).

In your example I would add log to this list of uninflected words. This means that system wide cake will not append the 's'. You'll have your LogController your views would sit in the Log view folder etc...

EDIT

I've come across a much neater way to do this from within app/config/bootstrap.php

Inflector::rules(
    'plural',
    array(
         'uninflected' => array('log')
    )
);

This would add log to the uninflected list without having to alter the files within the Core to allow easier version updating.

Confessor answered 18/10, 2012 at 14:44 Comment(1)
But that meanse I would be editing files in the core. Which I don't want. Because I want to be able to update Cake with a click on a button. So perhaps I have to extend the class, but that would really be a pain. I guess there are some nice methods for that. I will research it when I have time :) Tnx for the thinkingPodgorica
G
-1

You can simply do it using following:

class LogsController extends AppController
{
public $name = 'Log';
..... YOUR REMAINING CODE ......
}

Your router connnect code will remain same. Kindly ask if it not worked for you.

Galenic answered 13/8, 2012 at 4:36 Comment(2)
I tried this, but it doesn't seem to work. I changed the $name in FlightsController to 'Flight', but when I try to access example.com/flight it says that the FlightController is missing.Podgorica
No, the name variable is for backward compatibility with PHP4. It will have no impact on routing cause the controller is loaded "after" routing is performed.Manganite

© 2022 - 2024 — McMap. All rights reserved.