Laravel-在路线中添加斜线 [英] Laravel - append a trailing slash in routes
问题描述
正如我在这里多次提到的,我正在使用Laravel重做我的一个旧站点.
As I mentioned several times here, I'm redoing one of my old sites with Laravel.
我遇到的另一个问题如下:
Another problem I encounter is the following :
- 我在旧网站上主要有两种不同类型的网址:
-
/user-slug/
用于用户主页,以及 -
/user-slug/content-slug.html
为其内容之一
- I've got mainly two different types of urls on the old website :
/user-slug/
for the users homepage, and/user-slug/content-slug.html
for one of its content
我设法重新创建了第二个,但是当我尝试创建到用户路由的链接时,Laravel总是修剪最后一个斜杠,并且我的URL以
/user-slug
I managed to recreate the second one, but Laravel always trims the last slash when I try to create a link to the user route, and I end with an url like
/user-slug
在将旧网站切换到Laravel时,我不想失去任何SEO,所以我想知道是否有可能迫使Laravel在一个网址上附加斜杠?
I don't want to lose any SEO when switching the old website to Laravel, so I wanted to know whether it was possible to force Laravel to append the trailing slash on one url ?
到目前为止,我的代码:
My code so far:
app \ Providers \ RouteServiceProvider.php:
app\Providers\RouteServiceProvider.php :
public function boot(Router $router) { $router->pattern('user', '[a-zA-Z0-9_-]+'); $router->pattern('content', '[a-zA-Z0-9_-]+'); parent::boot($router); }
app \ Http \ routes.php:
app\Http\routes.php :
Route::group(['middleware' => ['web']], function () { Route::get('/', [ 'as' => 'index', 'uses' => 'BaseController@index', ]); Route::get('/{user}/', [ 'as' => 'user', 'uses' => 'UserController@show', ]); Route::get('/{user}/{content}.html', [ 'as' => 'content', 'uses' => 'ContentController@show', ]); });
views/home.blade.php:
views/home.blade.php :
{!! Html::linkRoute('user', $content['user']['name'], [$content->user->slug]) !!}
如果没有解决方法,我将使用
.htaccess
文件将所有/user-slug/
网址重定向到/user-slug
,但是如果可以避免的话,那会很酷.
If there is no workaround, I'll use a
.htaccess
file to redirect all/user-slug/
urls to/user-slug
, but if I can avoid it, that would be cool.推荐答案
Humm,我认为通过中间件例如它看起来是可能的.
Humm I think it looks possible through middleware eg.
-
$ php artisan make:middleware slashesMiddleware
-
转到 app/Http/Middleware/slashesMiddleware.php ,并用此内容填充
$ php artisan make:middleware slashesMiddleware
go to app/Http/Middleware/slashesMiddleware.php and fill it with this content
<?php namespace App\Http\Middleware; use Closure; class slashesMiddleware { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next, $flag) { if ($flag=="remove") { if (ends_with($request->getPathInfo(), '/')) { $newval = rtrim($request->getPathInfo(), "/"); header("HTTP/1.1 301 Moved Permanently"); header("Location:$newval"); exit(); } } else { if (!ends_with($request->getPathInfo(), '/')) { $newval =$request->getPathInfo().'/'; header("HTTP/1.1 301 Moved Permanently"); header("Location:$newval"); exit(); } } return $next($request); } }
-
此添加2种方法首先添加" 尾部斜线以进行路由或删除" 它
this add 2 methods first "add" trailing slash to route or "remove" it
将其添加到您的应用中间件列表中,并转到 $ routeMiddleware
add it list of middlewares of you app go to app/Http/Kernel.php under the array $routeMiddleware
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'slashes' => \App\Http\Middleware\slashesMiddleware::class, ];
- 最终将其添加到所需的任何路由,也可以从 $ middleware 数组 将其添加到所有路由
- finnaly added it to any route you want or you can add it to all route from $middleware array
用法示例
Route::get('/compare', 'WebRouteController@compare')->middleware("slashes:add");
这篇关于Laravel-在路线中添加斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-