簡體   English   中英

Laravel 4 URL重寫

[英]Laravel 4 URL Rewrite

我有一條控制當前頁面用戶的路由:

Route::group(array('prefix'=>'v1'), function(){
    Route::resource('page', 'PageController', ['only'=>['index','show']]);
});

通過上面的代碼,這意味着要轉到主頁,我需要輸入類似http://localhost/public/v1/page 因此,我需要將其設置為http://localhost/public/v1因此我將上面的代碼更改為如下所示:

Route::group(array('prefix'=>'v1'), function(){
    Route::resource('/', 'PageController', ['only'=>['index','show']]);
});

這僅適用於http://localhost/public/v1 ,如果我導航到類似http://localhost/public/v1/our-products ,則會產生錯誤,指出未找到路由。 有什么解決方法(不使用.htaccess重寫)嗎?

另外,如果可能的話,我想剝離鏈接中的v1 ,但是路由中仍然存在api代碼,是否可能(再次,沒有htaccess)? 謝謝您的幫助。

編輯

這是我的PageController

    <?php

class PageController extends MediaController { //MediaController extends BaseController

    protected $layout = 'layouts.master';
    private $data = array();

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() //this one for index/home page
    {
        $data = array();
        $data['data'] = null;

        $css_files = $this->addCSS(array('styles'));
        $plugin_files = $this->addJqueryPlugin(array('unslider'));

        $data['css_files'] = $this->addCSS(array('styles'));

        if(!empty($plugin_files) && isset($plugin_files)) {
            $data['css_plugin'] = $plugin_files['css_files'];
            $data['js_plugin'] = $plugin_files['js_files'];
        }

        $data['js_files'] = $this->addJS(array('app'));
        $data['title'] = 'Homepage - PT Anugerah Bhandala Sejati';

        $this->layout->content = View::make('page.home', $data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) //this one for OTHER THAN home page (like news page or product page, so for example, the link "http://localhost/public/v1/our-products" logic will go here)
    {
        if($id === "our-products") {
            $data = array();
            $data['data'] = null;

            $css_files = $this->addCSS(array('styles'));
            $plugin_files = $this->addJqueryPlugin(array('unslider'));

            $data['css_files'] = $this->addCSS(array('styles'));

            if(!empty($plugin_files) && isset($plugin_files)) {
                $data['css_plugin'] = $plugin_files['css_files'];
                $data['js_plugin'] = $plugin_files['js_files'];
            }

            $data['js_files'] = $this->addJS(array('app'));
            $data['title'] = 'Our Products - PT Anugerah Bhandala Sejati';

            $this->layout->content = View::make('page.product', $data);
        }
        else 
            return "Page not found";
    }

}

從您的評論:

“您在上方看到的是當前的所有路線,我正在嘗試將/作為主頁,/ our-products作為產品類別頁面,our-products / item1作為產品特定頁面,/ news作為所有新聞頁面,/ news / news1 /新聞標題作為新聞特定頁面”

我了解的是這樣的。

在您的app/routes.php

Route::resource('our-services', 'ProductsController', ['only'=>['index','show']]);
Route::get('news/{cat}/{title}', 'NewsController@single'); //news single post
Route::get('news', 'NewsController@index'); //news listings page
Route::get('/', 'HomePageController@index'); //home page

您只需要先了解自己想要什么,然后一次完成一個步驟即可。

請注意,這將無法正常工作,但我希望這會使您更清楚


UPDATE

如果仍要將它們包裝在v1中,則:

Route::group(array('prefix'=>'v1'), function(){
    Route::resource('our-services', 'ProductsController', ['only'=>['index','show']]);
    Route::get('news/{cat}/{title}', 'NewsController@single'); //news single post
    Route::get('news', 'NewsController@index'); //news listings page
    Route::get('/', 'HomePageController@index'); //home page
});

更新2

從您的另一條評論中,我想到的是:

Route::resource('v1', 'PageController', ['only'=>['index','show']]); 

因此,您有一個http://localhost/public/v1和一個自動生成的http://localhost/public/v1/{resourceid}路由。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM