找不到路线,symfony
No route found, symfony
我正在学习 Symfonycast 的 symfony 教程,并且我正在学习投票系统部分。
我所有的 ajax 请求都是 404,因为 url 不正确,但我不知道如何自己修复它。我已经使用了网站提供的代码。
这是我的控制器:
<?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]);
}
}
这是使用的 javascript :
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);
});
});
如果我照写的那样POST http://127.0.0.1/comments/10/vote/up 404 (Not Found)
我想正确的 URL 应该是 127.0.0.1/dev/public/question/reversing-a-spell/comments/10/vote/up ?
我没有头绪,我被卡住了
我决定转储 xampp 并使用 symfony serve -d 启动服务器,然后提供的代码开始工作。
我正在学习 Symfonycast 的 symfony 教程,并且我正在学习投票系统部分。 我所有的 ajax 请求都是 404,因为 url 不正确,但我不知道如何自己修复它。我已经使用了网站提供的代码。
这是我的控制器:
<?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]);
}
}
这是使用的 javascript :
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);
});
});
如果我照写的那样POST http://127.0.0.1/comments/10/vote/up 404 (Not Found)
我想正确的 URL 应该是 127.0.0.1/dev/public/question/reversing-a-spell/comments/10/vote/up ?
我没有头绪,我被卡住了
我决定转储 xampp 并使用 symfony serve -d 启动服务器,然后提供的代码开始工作。