简体   繁体   中英

Building an ecommerce site using Laravel: How do I view/route products based on their ID?

I followed a tutorial on Tutsplus about creating an ecommerce website using Laravel. The problem I'm having right now is when trying to route to a subfolder. In the tutorial, the instructor included a feature where you can view products by ID. And this is how he did it:

// StoreController.php
public function getView($id) {
    return View::make('store.view')->with('store', Store::find($id));
}

This piece of code seems to be passing an id from the stores table. I think when a product is clicked, that's when the id is passed

// Routes.php
Route::controller('store', 'StoreController');

Also some of the templates:

// store\index.blade.php
<h2>Stores</h2>
<hr>
<div id="stores row">
    @foreach($stores as $store)
    <div class="stores col-md-3">
        <a href="/store/products/view/{{ $store->id }}">
            {{ HTML::image($store->image, $store->title, array('class' => 'feature', 'width'=>'240', 'height' => '127')) }}
        </a>

        <h3><a href="/store/products/view/{{ $store->id }}">{{ $store->title }}</a></h3>

        <p>{{ $store->description }}</p>
    </div>
    @endforeach
</div><!-- end product -->

So.. How it goes is when I click on a product, it leads me to domain:8000/store/view/6 where 6 is the id .

This works fine but what I want to know is how do I route through a subfolder? Let's say I want it to be like this: store/view/products/6 considering that I have a folder called products and my view.blade.php is inside that like this: store/products/view .

In my StoreController class, I tried changing this

public function getView($id) {
    return View::make('store.view')->with('store', Store::find($id));
}

to this

public function getView($id) {
    return View::make('store.product.view')->with('store', Store::find($id));
}

but it does not seem to work giving me nothing but a Controller Method Not Found Error.

First, the view name View::make('store.product.view') has nothing to do with the URL.

You have to change the route:

Route::controller('store/view', 'StoreController');

And then adjust the name of your method in the controller because it should be the same as the segment of the URL after store/view

public function getProducts($id) {
    return View::make('store.product.view')->with('store', Store::find($id));
}

I strongly recommend you read the Laravel docs on the topic

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