【问题标题】:symfony route not working but showing in console router:debugsymfony 路由不工作,但在控制台路由器中显示:调试
【发布时间】:2017-01-17 18:31:10
【问题描述】:

我在控制器中添加了一条路由,但即使我可以在控制台中的 router:debug 中看到它,它也不起作用。

/**
* @Route("/apilucky", name="lucky")
*/
public function numberAction(Request $request)
{
  // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
    return $this->json(array('username' => 'jane.doe'));
}

当我从浏览器或邮递员访问它时收到 404 消息,但是当我启动“php app/console router:debug”时,一切看起来都很好(来自现有/工作路由的副本输出):

not working route

working route (我必须做截图,复制/粘贴后格式不可读)

我是 symfony 的新手,但这看起来很容易解决。我忘记清除/刷新任何东西了吗? (我尝试运行“php app/console clear:cache”,但没有帮助)。


编辑: 客户端的缓存也被清除了。

我尝试访问的地址是 www.serverurl.fr/fr/apilucky

控制器文件很大(有很多其他路由(我没有设置它们)正在工作,但这里有一个摘录:

<?php

namespace App\CoreBundle\Controller;

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


/**
 * @Route("/")
 */
class WebviewController extends BaseController
{
// ...
    /**
 * @Route("/accounts", name="accounts")
 */
public function AccountsAction(Request $request)
{
    $accounts = $this->getDoctrine()->getManager()->getRepository('AppCoreBundle:Account')->findAll();
    $accountsArray = array();

    foreach ($accounts as $account) {
        $HasWeboob = $account->getWeboob();
        if ($HasWeboob) {
            $accountsArray[] = array(
                'id_weboob' => $account->getWeboob(),
                'account_name' => $account->getName(),
                'account_id' => $account->getId(),
                );
        }
    }
    return new JsonResponse($accountsArray);


 }
    /**
    * @Route("/apilucky", name="lucky")
    */
    public function numberAction(Request $request)
    {
      // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
        return $this->json(array('username' => 'jane.doe'));
    }

}

非常感谢。

干杯, 朱利安

解决方案:

我清除了服务器端的缓存并且它起作用了。 php app/console clear:cache --env=prod

【问题讨论】:

  • 请在您使用路由的地方分享您的控制器代码。
  • 抱歉,错过了复制/粘贴。我已将其放在原始消息中。
  • 你试过清除缓存吗? (命令/手动)
  • 您尝试访问的网址是什么?您的网络服务器是如何配置的?
  • 你有其他路由在这个控制器中工作吗?粘贴整个控制器可能会有所帮助。

标签: php symfony controller routes


【解决方案1】:

我认为问题可能出在@route 定义中,您将控制器中的路由定义为:

/**
* @Route("/apilucky", name="lucky")
*/
public function numberAction(Request $request)
{
  // your code here
}

但是您请求的是http://www.serverurl.fr/fr/apilucky,而您的路由定义中不存在locale 参数。

如果请求类似于http://www.serverurl.fr/fr/apiluckyhttp://www.serverurl.fr/apilucky,您应该将您的路线定义为:

/**
* @Route("/{_locale}/apilucky", name="lucky")
* @Route("/apilucky", name="otherLuckyName")
*/
public function numberAction(Request $request)
{
  // your code here
}

但是,如果您的请求将永远是http://www.serverurl.fr/fr/apilucky,那么您应该将其定义为:

/**
* @Route("/{_locale}/apilucky", name="lucky")
*/
public function numberAction(Request $request)
{
  // your code here
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2018-08-15
    • 2016-03-03
    • 2018-02-25
    • 2020-09-04
    相关资源
    最近更新 更多