Answer the question
In order to leave comments, you need to log in
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;
}
}
[
'class'=>'app\components\CategoryManager'
],
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'));
}
}
Answer the question
In order to leave comments, you need to log in
I found the solution myself, it was necessary to change the namespace from this
to thisuse yii\web\Controller;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question