是否可以在Laravel 5中的routes文件中重写routes
是否可以在Laravel 5的routes.php文件而不是my.htaccess文件中执行URL重写?我知道重定向很容易,但我最好避免这样做,以便在地址栏中保留短url 假设我有使用该路由的url:是否可以在Laravel 5中的routes文件中重写routes,routes,rewrite,laravel-5,Routes,Rewrite,Laravel 5,是否可以在Laravel 5的routes.php文件而不是my.htaccess文件中执行URL重写?我知道重定向很容易,但我最好避免这样做,以便在地址栏中保留短url 假设我有使用该路由的url: Route::get('store/{category}/{subcategory}/{product}', [ 'uses'=>'StoreController@getProduct' ]); 我想创建一个快捷URL,从数据库表中获取完整URL,然后调用正确的路由和参数,而无需重
Route::get('store/{category}/{subcategory}/{product}', [
'uses'=>'StoreController@getProduct'
]);
我想创建一个快捷URL,从数据库表中获取完整URL,然后调用正确的路由和参数,而无需重定向
Route::get('{slug}', function($slug){
$shortcut = \App\Shortcut::whereSlug($slug)->first();
// I'm making the execute_route() function up here
return execute_route($shortcut->full_url);
})
基本上是在一条路线中调用一条路线
这可能吗?我理解您想要实现的目标,我建议您对请求只使用一条路由,在控制器中处理数据库查询,并根据当前slug或抛出404,而不是尝试调用当前路由中的另一条路由 像这样:
Route::get('/{slug?}',array(
'uses' => 'ShortcutController@getSlug',
'as' => 'shortcut.getSlug'
));
<?php namespace App\Http\Controllers;
use \View;
use App\Shortcut;
class ShortcutController extends Controller {
public function getSlug($slug=null){
if(!$shortcut = Shortcut::whereSlug($slug)->first()) abort(404);
return view('slug')
->with('shortcut', $shortcut);
}
}
注意:上面的路由应该是routes.php上的最后一个列表,否则可能会匹配其他一些路由