簡體   English   中英

symfony路由不起作用,但在控制台路由器中顯示:debug

[英]symfony route not working but showing in console router:debug

我在控制器中添加了一條路由,但是即使可以從控制台在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 /控制台路由器:調試”時,一切看起來都很好(現有/工作路徑的復本輸出):

不工作路線

工作路線 (我必須做屏幕截圖,在此處復制/粘貼后無法讀取格式)

我是symfony的新手,但這看起來很簡單。 我忘了清除/刷新任何東西嗎? (我嘗試運行“ php app /控制台清除:緩存”,但沒有幫助)。


編輯:客戶端上的緩存也被清除。

我嘗試訪問的地址是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應用程序/控制台清除:緩存--env = prod

我認為問題可能出在@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/apilucky並且也類似於http://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
}

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM