Home > Software design >  No route found, symfony
No route found, symfony

Time:10-01

I am following Symfonycast's symfony tutorial and I'm up to the voting system part. All my ajax requests are 404 because the url is not right but I have no clue how to fix it myself. I have used the code provided by the site.

Here is my controller :

<?php
namespace App\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class CommentController extends AbstractController
{
    /**
     * @Route("/comments/{id}/vote/{direction<up>|<down>}", name="app_comment_show", methods="POST")
    */
    public function commentVote($id, $direction, LoggerInterface $logger)
    {

        if ($direction === 'up') {
            $logger->info('Voting up');
            $currentVoteCount = rand(7, 100);
        } else {
            $logger->info('Voting down');
            $currentVoteCount = rand(0, 5);
        }
        return $this->json(['votes' => $currentVoteCount]);
    }
}

Here is the javascript used :

 var $container = $('.js-vote-arrows');
 $container.find('a').on('click', function(e) {
     e.preventDefault();
     var $link = $(e.currentTarget);
 
     $.ajax({
         url: '/comments/10/vote/' $link.data('direction'),
         method: 'POST'
     }).then(function(data) {
         $container.find('.js-vote-total').text(data.votes);
     });
 });

If I let it as it's written POST http://127.0.0.1/comments/10/vote/up 404 (Not Found) I suppose the correct URL would be 127.0.0.1/dev/public/question/reversing-a-spell/comments/10/vote/up ?

I have no clue and I'm stuck

CodePudding user response:

I believe you have made an error in your inline parameter requirement, the syntax is {parameter_name<requirements>} where requirements is the entire regex enclosed within one set of < & >.
Try changing this part:

/**
 * @Route("/comments/{id}/vote/{direction<up>|<down>}", name="app_comment_show", methods="POST")
*/

To this:

/**
 * @Route("/comments/{id}/vote/{direction<up|down>}", name="app_comment_show", methods="POST")
*/

https://symfony.com/doc/current/routing.html#parameters-validation

CodePudding user response:

I decided to dump xampp and use symfony serve -d to launch the server and the provided code worked then.

  • Related