0

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

}
9
  • Could you make a list of the routes that you would like to have? I'm not sure if I understand what you are trying to do. Commented Nov 21, 2013 at 18:07
  • @Manuel: 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 and perhaps other routes for ajax request later (no membership here) Commented Nov 21, 2013 at 18:12
  • could you post on your question what routes/urls you want and show your routes.php file here? i'm not sure but it must be the order of your routes.. Commented Nov 21, 2013 at 18:21
  • Why did you add the array('prefix' => 'v1') part then? Commented Nov 21, 2013 at 18:21
  • 1
    something like Route::resource('v1', 'PageController', ['only'=>['index','show']]); so you have a http://localhost/public/v1 and a http://localhost/public/v1/{resourceid} routes auto generated Commented Nov 21, 2013 at 18:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

yes, i realize with Route::get i can do just that, I just want to know if it possible without all those Route::get (as you see in my edit, show method will handle other than /routes)
using Resource Controllers would mean you automatically add the two the resource routes with it.. if you want a custom route not in that list.. (in your case index and show) put it before the Route::Resource declaration since it may have a conflict (need more info on here)..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.