A
A
Alexey Alekseev2019-03-14 19:06:51
Yii
Alexey Alekseev, 2019-03-14 19:06:51

How to display the goods of the selected category of the online store?

I created CategoryController.php in the controllers folder, everything seems to be fine, I can’t display the product of the selected category from the database, it displays the error "http ://site.loc/category/3"
Here is the code in CategoryController.php

<?php


namespace app\controllers;
use app\models\Category;
use app\models\Product;
use Yii;


class CategoryController extends AppController
{
public function actionIndex(){
    $hits = Product::find()->where(['hit'=>'1'])->limit(4)->all();
    $this->setMeta('yiishoptest');
    return $this->render('index', compact('hits'));
}
public function actionView($id){
    $id = Yii::$app->request->get('id');
    $products = Product::find()->where(['category_id'=>$id])->all();
    $category = Category::findOne($id);
    $this->setMeta
    ('yiishoptest | . $category->name,
     $category->keywords, $category->description,');
    return $this->render('view', compact('products', 'category'));

  }
}

and here is the web.php code in the config folder
'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'baseUrl' => '//site.loc',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'category/<id:\d+>'=> 'category/view',
            ],

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question