O
O
Outsider V.2017-07-02 10:08:26
symfony
Outsider V., 2017-07-02 10:08:26

How to scatter routing in Symfony 3?

I'm learning Symfony, I just can't handle routing through annotations and yml configs.
It is necessary that all routing in my asoft/OrganizerBundle project be annotated in controllers.
Registered:
E:\Program Files\Apache24\htdocs\symfony.local\organizer\app\config\routing.yml

organizer:
    resource: "@OrganizerBundle/Resources/config/routing.yml"

app:
    resource: '@AppBundle/Controller/'
    type: annotation

E:\Program Files\Apache24\htdocs\symfony.local\organizer\src\asoft\OrganizerBundle\Resources\config\routing.yml
organizer:
    resource: "@OrganizerBundle/Controller/"
    type:     annotation

E:\Program Files\Apache24\htdocs\symfony.local\organizer\src\asoft\OrganizerBundle\Controller\DefaultController.php
<?php
declare(strict_types = 1);

namespace asoft\OrganizerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     */
    public function showMainAction(): Response
    {
        return $this->showDayAction((new \DateTime())->format('Y-m-d'));
    }


    /**
     * @Route("/{day}")
     */
    public function showDayAction(string $day): Response
    {
        echo $day;
        return $this->render('OrganizerBundle:Default:index.html.twig');
    }
}

When going to the main page, it writes in the log:
request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "The controller for URI "/" is not callable. Expected method "indexAction" on class "asoft\OrganizerBundle\Controller\DefaultController". Available methods: "showMainAction", "showDayAction", "setContainer"." at E:\Program Files\Apache24\htdocs\symfony.local\organizer\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Controller\ControllerResolver.php line 98 {"exception":"[object] (InvalidArgumentException(code: 0): The controller for URI \"/\" is not callable. Expected method \"indexAction\" on class \"asoft\\OrganizerBundle\\Controller\\DefaultController\". Available methods: \"showMainAction\", \"showDayAction\", \"setContainer\". at E:\\Program Files\\Apache24\\htdocs\\symfony.local\\organizer\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver.php:98)"} []

When navigating to /2007-05-02 :
request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /2007-05-02"" at E:\Program Files\Apache24\htdocs\symfony.local\organizer\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\EventListener\RouterListener.php line 125 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /2007-05-02\" at E:\\Program Files\\Apache24\\htdocs\\symfony.local\\organizer\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\EventListener\\RouterListener.php:125, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0):  at E:\\Program Files\\Apache24\\htdocs\\symfony.local\\organizer\\var\\cache\\prod\\appProdProjectContainerUrlMatcher.php:56)"} []

I'm already bored. It seems that I imported the config of my bundle into the main route config, and in it I registered that all routing goes through annotations in the controllers (the Controller folder). What's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Outsider V., 2017-07-02
@Audiophile

It all worked when I wrote in the global and bundled routing.yml, respectively:

organizer:
    resource: '@OrganizerBundle/Resources/config/routing.yml'

organizer:
    resource: "@OrganizerBundle/Controller/DefaultController.php"
    type: annotation

After clearing the cache, production started working.

D
Denis, 2017-07-02
@denis6064

In the showMainAction annotation, assign the name parameter. You should get something like:

/**
     * @Route("/", name="show_main")
     */

And for showDayAction, you need to change the routing a little and do something like this:
/**
     * @Route("/day/{day}", name="show_day")
     */

Because it turns out that you are referring to the same action

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question