0

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.

Rayann Nayran
  • 1,135
  • 7
  • 15
libarra
  • 3
  • 2
  • 2
    Just don't do it, whether you use URLs with or without trailing slash [**has no effect on SEO**](https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html), just make sure that you consistently stick with one or the other. – ndm May 29 '17 at 20:29
  • Thank you for your advise. Probably I didn't express myself properly. When I say "for SEO purposes", I mean that I would like to stick with the trailing slash URL structure, not that the trailing slash itself would help my SEO. – libarra May 29 '17 at 20:44
  • I agree with @ndm - but see https://github.com/dereuromark/cakephp-trailing-slash – mark May 30 '17 at 13:42
  • @mark thank you for advise and your refference. I have found that code before asking the question. I tried to use it but it was kind of troublesome as it is meant to be used in CakePHP 2.x. In fact, all the code and workarounds I found regarding this "issue" were meant for CakePHP 2.x. Thank you again. – libarra May 30 '17 at 16:09

3 Answers3

0

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

Rayann Nayran
  • 1,135
  • 7
  • 15
  • I already have the URL like that for it to work, but what I need is to craft the URL with the trailing slash using the Controller and Action option. ['controller' => 'Messages', 'action' => 'send'] I would like to do this in order to avoid tracking down each URL set through this method in case I decide to change the request alias in the future – libarra May 29 '17 at 19:55
0

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 App\View\Helper;

use Cake\View\Helper\UrlHelper;

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.

Marijan
  • 1,825
  • 1
  • 13
  • 18
  • Thank you very much! That was exactly what I needed. I had to change `MyUrlHelper.php` a little bit for it to work: I added `use Cake\Routing\Router` and extended `MyUrlHelper` from `UrlHelper` instead of extending from `HtmlHelper`. Thanks again! This is a great solution. – libarra May 30 '17 at 15:56
0

I SOLVED it by three characters :) ...

1- open \vendor\cakephp\cakephp\src\View\Helper\UrlHelper.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

caspero
  • 1
  • 3
  • I'm sorry this will break the image ... but I put it in ..... \vendor\cakephp\cakephp\src\View\Helper\HtmlHelper.php -> link function ... it works fine – caspero Jan 14 '21 at 12:45