I'm using Cakephp 2 I've been re factoring my code to use the Html->url() function instead of hard coding web addresses. For those not familiar, I pass a mixed $url variable containing controller and action names.
My gut tells me I should be defining the controller according to the file name and the action according to the function name inside the controller. ie: To route to AdminsController::index() I would say
$this->Html->url(array(
'controller'=>'Admins'
,'action'=>'index'
));
and that SHOULD generate for me the url
http://example.com/admins/index
Unfortunately, what it is generating for me is
http://example.com/Admins/index
*notice the uppercase "A" in admins.
The purest in me refuses to identify the controller by its inflected name because then what's the advantage of using the url helper function at all? Why wouldn't I just write out the urls myself?
Shouldn't the url function be inflecting the controller names? Isn't lower case / camel case a part of the inflection process? Is there any way I can force this behavior?
Thanks
If you define the following route in routes.php, you will then have your nice lowercase url :
Router::connect('/admins/index', array(
'controller' => 'Admins',
'action' => 'index'
));
EDIT: other suggestion, you can also look into defining a custom route that would automatically lower case action and controller when parsing a route url: http://book.cakephp.org/2.0/en/development/routing.html#custom-route-classes
EDIT2: just got an other idea. You can wrap Html helper into a custom one that performs what you want.
In View/Helper/CustomHtmlHelper.php :
<?php
App::uses('HtmlHelper','View/Helper');
class CustomHtmlHelper extends HtmlHelper {
public function url($url = null, $full = false)
{
if(is_array($url)) {
if(isset($url['controller']) {
$url['controller'] = strtolower($url['controller']);
}
if(isset($url['action']) {
$url['action'] = strtolower($url['action']);
}
}
return parent::url($url, $full);
}
In Controller/AppController.php :
public $helpers = array('Html' => array('className' => 'CustomHtml'));
I haven't tested, so there might be errors in code. But this is the idea.
If you want the to use the HtmlHelper, you will need to always format your actions/controllers and plugins thus:
$this->Html->url(array(
'controller'=>'admins'
,'action'=>'index'
));
or:
$this->Html->url(array(
'controller'=>'controllers'
,'action'=>'some_other_action'
));
Cake will take care of inflecting everything. Just pass the lowercased underscored versions. Invluding if your action is (someOtherController() in the above example)
It's the same in your URLs - you access /admins/, not /Admins/ - the helper works the same way.