简体   繁体   English

Laravel 4 URL重写

[英]Laravel 4 URL Rewrite

I have a route that controls the current page user is in: 我有一条控制当前页面用户的路由:

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

By the above code, it means that to go to homepage I need to type something like http://localhost/public/v1/page . 通过上面的代码,这意味着要转到主页,我需要输入类似http://localhost/public/v1/page So I need to make it to http://localhost/public/v1 so I changed the above code to something like this: 因此,我需要将其设置为http://localhost/public/v1因此我将上面的代码更改为如下所示:

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

That works only for http://localhost/public/v1 , if i navigate to something like http://localhost/public/v1/our-products it will produce error says route not found. 这仅适用于http://localhost/public/v1 ,如果我导航到类似http://localhost/public/v1/our-products ,则会产生错误,指出未找到路由。 Is there any workaround for this (without using .htaccess rewrite)? 有什么解决方法(不使用.htaccess重写)吗?

Additionally I would like to strip the v1 in the link if possible, but the api code still there in the route, is it possible (again, without htaccess)? 另外,如果可能的话,我想剥离链接中的v1 ,但是路由中仍然存在api代码,是否可能(再次,没有htaccess)? Thanks for the help. 谢谢您的帮助。

EDIT 编辑

Here's my PageController : 这是我的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";
    }

}

from your comment: 从您的评论:

"What you see above is currently all my routes, i'm trying to make / as home page, /our-products as product categories page, our-products/item1 as product specific page, /news as all news page, /news/news1/title-of-the-news as news specific page" “您在上方看到的是当前的所有路线,我正在尝试将/作为主页,/ our-products作为产品类别页面,our-products / item1作为产品特定页面,/ news作为所有新闻页面,/ news / news1 /新闻标题作为新闻特定页面”

what i understand is something like this.. 我了解的是这样的。

On your app/routes.php 在您的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

you just need to understand what you want first, then do things one step at a time.. 您只需要先了解自己想要什么,然后一次完成一个步骤即可。

take note that this will not work as is, but i hope this will make things clearer for you 请注意,这将无法正常工作,但我希望这会使您更清楚


UPDATE UPDATE

If you still want to wrap them in v1, then: 如果仍要将它们包装在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
});

UPDATE 2 更新2

from another comment of yours, here's what i have come up: 从您的另一条评论中,我想到的是:

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

so you have a http://localhost/public/v1 and a http://localhost/public/v1/{resourceid} routes automatically generated then.. 因此,您有一个http://localhost/public/v1和一个自动生成的http://localhost/public/v1/{resourceid}路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM