简体   繁体   English

Laravel中有没有办法让所有链接/重定向都带有斜杠?

[英]Is there a way in Laravel for all links/redirects to be with trailing slash?

I'm redesigning old website which is built with custom code, and now we are using Laravel framework (version 5.3). 我正在重新设计使用自定义代码构建的旧网站,现在我们正在使用Laravel框架(5.3版)。 The problem is that old website has all links with trailing slashes. 问题是旧网站的所有链接都带有斜杠。 It has great SEO and many visits from search engines so removing trailing slashes is not the option. 它具有出色的SEO以及来自搜索引擎的多次访问,因此不能选择删除斜杠。

In my routes/web.php file i have following routes: 在我的routes / web.php文件中,我有以下路由:

$router->get('texts/', 'TextController@index');
$router->get('texts/{slug}/', 'TextController@category');
$router->post('texts/search/', 'TextController@searchPost');
$router->get('texts/search/', 'TextController@search');

Showing links on html/blade views with trailing slashes is not the problem, problem is redirecting to route links. 在html / blade视图上显示带有斜杠的链接不是问题,问题是重定向到路由链接。

App/Http/Controllers/TextController.php App / Http / Controllers / TextController.php

public function searchPost()
{
    ...

    return $this->response->redirectToAction('TextController@search');
}

This redirect me to " texts/search " instead on " texts/search/ ". 这将我重定向到“ texts / search ”,而不是“ texts / search / ”。 Is there any options to turn on/off trailing slashes in Laravel or some hacky way to fix this ? 是否有任何选项可以在Laravel中打开/关闭尾部斜杠或某些解决方法? .htaccess redirect is not the solution because it adds one more redirect and slows down website. .htaccess重定向不是解决方案,因为它增加了一个重定向并降低了网站速度。

I think this could be useful https://www.neontsunami.com/posts/redirect-trailing-slashes-in-laravel-5 我认为这可能很有用https://www.neontsunami.com/posts/redirect-trailing-slashes-in-laravel-5

Just adjust it to your needs, you can remove the rtrim function call and try the Redirect::to method. 只需根据需要对其进行调整,就可以删除rtrim函数调用,然后尝试Redirect :: to方法。

Figured it out, I needed to extend UrlGenerator class. 想通了,我需要扩展UrlGenerator类。

Created TrailingSlashUrlGenerator.php inside App/Library folder: App / Library文件夹中创建了TrailingSlashUrlGenerator.php

namespace App\Library;

use Illuminate\Routing\UrlGenerator;

class TrailingSlashUrlGenerator extends UrlGenerator
{
    /**
     * Format the given URL segments into a single URL.
     *
     * @param  string  $root
     * @param  string  $path
     * @param  string  $tail
     * @return string
     */
    protected function trimUrl($root, $path, $tail = '')
    {
        return parent::trimUrl($root, $path, $tail).'/';
    }
}

Create RoutingServiceProvider in App/Providers folder: App / Providers文件夹中创建RoutingServiceProvider:

public function register()
{
    $this->app['url'] = $this->app->share(function($app) {
        $routes = $app['router']->getRoutes();
        $app->instance('routes', $routes);

        $url = new TrailingSlashUrlGenerator(
            $routes, $app->rebinding('request', $this->requestRebinder())
        );

        $url->setSessionResolver(function ($app) {
            return $app['session'];
        });

        return $url;
    });
}

Register provider in config/app.php : config / app.php中注册提供者:

App\Providers\RoutingServiceProvider::class,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM