duanba8173 2017-01-17 18:31
浏览 68

symfony路由不工作但在控制台路由器中显示:debug

I've added a route in my controller but it does not work even though i can see it in router:debug from the console.

/**
* @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'));
}

I get a 404 message when i visit it from my browser or postman, but when I launch "php app/console router:debug " everything looks fine (carbon copy output from a existing/working route):

not working route

working route (i had to do screenshots, formatting was not readable after copy/paste here)

I'm new to symfony but this looks like an easy fix. Did I forget to clear/refresh anything ? (I tried running "php app/console clear:cache" but it did not help).


edit: Cache was cleared on client as well.

The address i'm trying to access is www.serverurl.fr/fr/apilucky

The controller file is huge (there is a ton of other routes (i did not set them up) that are working, but here's an extract:

<?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'));
    }

}

Thanks a lot.

Cheers, Julien

SOLUTION:

I cleared the cache on the server side and it worked. php app/console clear:cache --env=prod

  • 写回答

1条回答 默认 最新

  • drg14799 2017-01-18 10:47
    关注

    I think the problem could be in the @route definition, you define your route in the controller as:

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

    But you are requesting http://www.serverurl.fr/fr/apilucky and the locale parameter doesn't exists in your route definition.

    If request will be like http://www.serverurl.fr/fr/apilucky and also like http://www.serverurl.fr/apilucky you should to define your routes as:

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

    But if your request will be ever as http://www.serverurl.fr/fr/apilucky then you should define it as:

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

    Hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题