1

I want to learn symfony and I start to create a small application. I have a question related to the routes. So, I have in my project the routes :

/admin/homepage, /admin/news, admin/galery

No if I write in url /admin, this route doesn't exist and I get as error No route found for "GET /admin/". Exist a way to check if route doesn't exist and redirect to another route for example ? Thx in advance and sorry for my english My routes :

news_all:
path: /news/all/{page}
defaults: { _controller: AppAdminBundle:News:all, page: 1 }
requirements:
    page: \d+
    _method:  GET|POST
news_add:
path: /news/add
defaults: { _controller: AppAdminBundle:News:add }
Harea Costicla
  • 797
  • 3
  • 9
  • 20

2 Answers2

0

In your case the best solution would be to override default ExceptionController and add custom logic there, e.g. redirection to other page - according to the docs: http://symfony.com/doc/current/cookbook/controller/error_pages.html#overriding-the-default-exceptioncontroller

# app/config/config.yml
twig:
    exception_controller:  AppBundle:Exception:showException

Note: Instead of creating a new exception controller from scratch you can, of course, also extend the default ExceptionController. In that case, you might want to override one or both of the showAction() and findTemplate() methods. The latter one locates the template to be used.

Kamil Adryjanek
  • 3,238
  • 1
  • 20
  • 21
  • sorry but I don't understand how the redirect will be maked if I will accees a non exist route – Harea Costicla Dec 30 '15 at 12:20
  • When you access route that does not exists, `HttpNotFoundException` is fired, than that exception is catched by `Symfony2` exception listener and `ExceptionConttroller` will be called. All you have to do is to implement your custom `ExceptionController` and redirect user to proper route with simple `$this->redirectToToute('your_route')` for example. – Kamil Adryjanek Dec 30 '15 at 12:49
0

Symfony's good practise are to set rout in your controller, using annotations

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

and

/**
* @Route("/news/add", name="news_add")
*/
public function addAction()
{
    // ...
}

Also, with annotation, you can set rout for a whole controller. Which is, in your case, what you're looking for.

/**
* @Route("/admin")
*/
class NewsController extends Controller
{
    /**
     * @Route("/news/add", name="news_add")
     */
    public function addAction()
    {
        // ...
    }
}

Also, I advice you to take a look at @Template annotation.

Else, to answer your question, I think you can make a custom twig function (check this link for more information). Function that checks is the given name a valid route:

function routeExists($name)
{
    // I assume that you have a link to the container in your twig extension class
    $router = $this->container->get('router');
    return (null === $router->getRouteCollection()->get($name)) ? false : true;
}
Bart Bartoman
  • 756
  • 4
  • 14
  • Can you please help? http://stackoverflow.com/questions/39715888/symfony-2-router-with-parameter-returns-404/ – Volatil3 Sep 27 '16 at 05:09