PhalconPHP - redirect to initialize ()

In my project, I created an AjaxController that managed ajax requests. I would like the user to enter the URL used for the ajax get 404 error. In AjaxController.php I have:

public function initialize() {
    if (!$this->request->isAjax()) {
        return $this->response->redirect('error/show404');
    }
}

(Of course I have an ErrorController with show404Action)

This does not work. When I enter example.com/ajax in a browser, I get content from IndexAction in AjaxController. How to restore it?

+4
source share
2 answers

Try to do the same in beforeExecuteRoute(). Phalcon is initialize()designed, as its name is, to initialize things. You can send there using the dispatcher, but do not have to redirect.

. " ?" , false, .

- , beforeExecuteRoute() , , .

public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
{
    if (!$this->request->isAjax()) {
        return $this->response->redirect('error/show404');
    }
}
+3

404 . , URL- , SEO.

public function initialize() {
    if (!$this->request->isAjax()) {
        $this->dispatcher->forward(['controller' => 'error', 'action' => 'show404']);
    }
}

. Phalcon : https://forum.phalconphp.com/discussion/3216/redirect-initialize-beforeexecuteroute-redirect-to-initalize-and

404 , - . ( SEO)

// 404
public function error404Action()
{
    $this->response->setStatusCode(404, 'Not Found');
    $this->view->pick(['templates/error-404']);
    $this->response->send();
}
0

All Articles