2015-04-20 49 views
0

是否有可能进行URL重写,但在Laravel 5的routes.php文件文件,而不是我的.htaccess文件?我知道这很容易与重定向,但我最好避免这样做,以保持地址栏中的短网址。是否有可能改写Laravel 5条航线,在routes文件

说我有URL“http://website.com/store/clothing/shites/cool-shirt”,将使用的路线:

Route::get('store/{category}/{subcategory}/{product}', [ 
    'uses'=>'[email protected]' 
]); 

我想创建一个快捷方式URL“http://website.com/cool-shirt”会从数据库表中提取完整的URL,然后调用正确的路线和参数(没有重定向)。

基本上呼叫路线内的路线。

这可能吗?

回答

0

我知道你想达到的,而不是试图调用本一个内部的另一条路线,是什么,我建议你使用的要求只有一个路由,手柄控制器和显示数据库查询和返回基于视图在当前块或抛出一个404

像这样:

Route::get('/{slug?}',array(
    'uses' => '[email protected]', 
    '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文件否则一些其他途径可能匹配

去年上市
相关问题