K
K
Katya Smirnova2019-05-14 19:44:03
Yii
Katya Smirnova, 2019-05-14 19:44:03

Forming urls yii2?

Good evening everyone, the customer, for no reason at all, wanted to change the category/build url to just build .
Naturally, for this you need to create a handler class.

namespace app\components;

use yii\web\UrlRuleInterface;
use yii\base\BaseObject;
use app\models\Category;

class CategoryManager extends BaseObject implements UrlRuleInterface
{
    public function createUrl($manager, $route, $params)
    {
        return false;
    }


    public function parseRequest($manager, $request)
    {
        $alias = $request->getPathInfo();
        $category = Category::find()->where(['alias' => $alias])->one();
        if ($category != NULL) {
        	return [
        		'category/view',
        		[
        			'category' => $category
        		]
        	];
        }else return false;
        // return ($category)? ['category/view', compact('category')] : false;
    }
}

In config/web.php added:
[
         'class'=>'app\components\CategoryManager'
],

Well, the controller:
namespace app\controllers;
use app\models\{ Build, Category };

use yii\base\Controller;
use yii\web\NotFoundHttpException;
use Yii;

class CategoryController extends Controller
{

  public function actionView(Category $category)
  {
    return $this->render('view', compact('category'));
  }

  public function actionIndex() {
    $builds = Build::find()->all();
    return $this->render('index', compact('builds'));
  }
}

It seems that I did everything according to the example of the documentation, but in the end I get an error
5cdaefb21ee3d525373330.png
What did I do wrong?
Why is the parameter not being passed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Katya Smirnova, 2019-05-14
@sireax

I found the solution myself, it was necessary to change the namespace from this to this
use yii\web\Controller;

R
Rsa97, 2017-05-04
@drtvader

append_block = $('js-product-list');
Are you sure you have an element with the js-product-list tag in your HTML?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question