简体   繁体   中英

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 . So I need to make it to http://localhost/public/v1 so I changed the above code to something like this:

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. Is there any workaround for this (without using .htaccess rewrite)?

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)? Thanks for the help.

EDIT

Here's my 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"

what i understand is something like this..

On your 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

If you still want to wrap them in v1, then:

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

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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