A
A
Arman2017-08-01 21:34:48
Yii
Arman, 2017-08-01 21:34:48

How Yii2 check URL before 404 error in content pages?

It was already advised to list the known controllers, but they are often added, as it is possible to leave the standard mechanism / check in the content pages before 404 showing, i.e. the last one will be [email protected] and if it throws a 404 exception, then only show a 404 error
thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-08-02
@Arik

In the application config, you can redefine the action for errors and describe any logic in it.

'errorHandler' => [
            'errorAction' => 'somecontroller/someaction',
        ],

www.yiiframework.com/doc-2.0/yii-web-errorhandler....
Only it's not entirely clear what you mean by "check in content pages".

I
Igor Vasiliev, 2017-08-02
@Isolution666

Hello.
--
I understand that you form links not by id, but by some column in the database, which has a string value.
so, if you are making a get request, then it's better to do it like this:

// код action
...
public function actionName($link= ''){ // обращаемся к той переменной, которая выводит get
$model = Catalog::find()->where(['link' => $link])->one(); // обращаемся к БД с вопросом, есть ли такая запись
if ($link == NULL) {return $this->redirect(['catalog']);}  // если нету, то делаем редирект*
...

---
* - if you insert the last line, you avoid the 404 page, the method of random selections to non-existent pages, and as a result, interfere with hackers with injections, the user remains on the site, and can choose something else, and not try to understand what they mean these three numbers. The page with empty get requests is not displayed, which is very important!!! You can redirect wherever you want.
I'll be glad to know that it helped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question