Подтвердить что ты не робот

Как я могу имитировать ошибку 404 с помощью Symfony2?

поэтому я ищу способ имитировать ошибку 404, я пробовал это:

throw $this->createNotFoundException();  

и этот

return new Response("",404);

но никто не работает.

4b9b3361

Ответ 1

Вы можете найти решение в документации Symfony2:

http://symfony.com/doc/2.0/book/controller.html

Управление ошибками и 404 страницы

public function indexAction()
{
    // retrieve the object from database
    $product = ...;
    if (!$product) {
        throw $this->createNotFoundException('The product does not exist');
    }

    return $this->render(...);
}

В документации есть короткая информация:

"Метод createNotFoundException() создает специальный объект NotFoundHttpException, который в конечном итоге вызывает 404 HTTP-ответ внутри Symfony."

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException

В моих сценариях я сделал это так:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException

/**
 * @Route("/{urlSlug}", name="test_member")
 * @Template()
 */
public function showAction($urlSlug) {
    $test = $this->getDoctrine()->.....

    if(!$test) {
        throw new NotFoundHttpException('Sorry not existing!');
    }

    return array(
        'test' => $test
    );
}