Laravel 4 URL Rewrite


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";
    }
}

来自您的评论:

"你在上面看到的是我目前的所有路线,我正在尝试将/作为主页,/我们的产品作为产品类别页面,我们的产品/item1作为产品特定页面,/新闻作为所有新闻页面,/消息/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

你只需要先了解自己想要什么,然后一步一个脚印地做事。。

请注意,这不会像现在这样工作,但我希望这会让你更清楚


更新

如果你仍然想把它们包装在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}路由,然后自动生成。。