skip to Main Content

I’m working on a blog-like website in CakePHP 3 and made the canonical URL structure with a trailing slash for SEO purposes. To accomplish this, in the routes file I built the requests to match a trailing slash, and in the webroot .htaccess made the proper redirects to handle requests without a trailing slash. Also, in the AppController I override the redirect function to manage redirects from Controllers:

function redirect($url, $status = null, $exit = true)
{
    $routerUrl = Router::url($url, true);
    if(!preg_match('/.[a-z0-9]{1,5}$/', strtolower($routerUrl)) && substr($routerUrl, -1) != '/') {
        $routerUrl .= '/';
    }
    parent::redirect($routerUrl, $status, $exit);
}

So far, so good.

Now, I would like to build URLs with a trailing slash every time I build them with a Helper, like FormHelper or HtmlHelper. For example:

$this->Form->create(null, [
    'url' => ['controller' => 'Messages', 'action' => 'send']
]);

The URL output in this case would be:

/messages/send

And I need it to be:

/messages/send/

At the moment I’m hard-coding the URL in the Helper’s options for it to work (not in production yet). If I use the example’s option, when the Form is submitted it redirects /messages/send to /messages/send/ because of the .htaccess redirect rules and the POST data is lost.

Thanks in advance, and I apologize for my poor English, I hope I made myself clear.

3

Answers


  1. If you want to add a / at the end of the action, you could try it:

    $this->Form->create(null, [
        'url' => $this->Url->build('/Messages/send/', true)
    ]);
    

    See also

    Login or Signup to reply.
  2. Iā€™d recommend to create an alias for your Helper and tweak the URL building.

    src/View/AppView.php

    class AppView extends View
    {
        public function initialize()
        {
            $this->loadHelper('Url', [
                'className' => 'MyUrl'
            ]);
        }
    }
    

    src/View/Helper/MyUrlHelper.php

    namespace AppViewHelper;
    
    use CakeViewHelperUrlHelper;
    
    class MyUrlHelper extends HtmlHelper
    {
        public function build($url = null, $options = false)
        {
            // Add your code to override the core UrlHelper build() function
    
            return $url;
        }
    }
    

    Find the original source of UrlHelper here.

    Login or Signup to reply.
  3. I SOLVED it by three characters šŸ™‚ …

    1- open vendorcakephpcakephpsrcViewHelperUrlHelper.php
    2- go to build function and add .’/’ to the return $url like below

    it should look likes
    return $url.’/’;

    instead of
    return $url;

    I know it’s dirty solution and not good to edit core … I think you’d better overwrite build function…

    I hope there is better way

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search