2014-10-22 140 views
1

關於Silex路由的一個簡單疑問。我很新的Silex的,基本上我學習它,就一切都這麼好,這裏是問題(??) -Silex路由問題

這是我的index.php - >

require_once __DIR__.'/../vendor/autoload.php'; 
$app = new Silex\Application(); 

$app->register(new Silex\Provider\UrlGeneratorServiceProvider()); 
use Symfony\Component\HttpFoundation\Response; 

$app->get('/', function() { 
    return 'Hello World!'; 
}); 

$app->get('/hello', function() { 
    return 'Hello From HELLO!'; 
}); 

$app->error(function (\Exception $e, $code) use($app) { 
    switch ($code) { 
     case 404: 
      $message = "Oooops Not Found"; 
      break; 
     default: 
      $message = $app['twig']->render('error500.html.twig'); 
    } 
    return new Response($message, $code); 
}); 

$app->run(); 

問題是關於路由問題,只要我們嘗試訪問家庭或/你好(http://mysite.devhttp://mysite.dev/hello),它就會工作。但如果我嘗試訪問 的鏈接,它不存在,像這樣 - http://mysite.dev/hello/blah它會按預期的方式返回404頁面($ app-> error()),但是如果我刪除/ blah部分並嘗試再次輸入在瀏覽器(http://mysite.dev/hello) - 仍然我得到404,訪問該網站我必須一路回到根(即http://mysite.dev)。我不知道如果我錯過了一些配置或其他的東西,或者可能是一個愚蠢的,但請我是一個在編碼起動。

這裏是一個很好的例子: - 去https://getcomposer.org/doc/00-intro.md它會導致你的作曲家文件入門頁面,如果你在這個網址這樣https://getcomposer.org/doc/00-intro.md/blah的末尾添加一些東西 - 它會給你「對不起,該頁面你正在尋找找不到的東西。'錯誤,好吧,如果你想回到入門頁面,如果你嘗試刪除/等等,並再次輸入,沒有辦法你仍然得到相同的錯誤頁面,任何人都可以解釋這一點。

同樣在這裏http://silex.sensiolabs.org/doc/

在此先感謝。

回答

1

該路線不期望後面的正斜槓。 Thsi是預期的行爲,並在長度已經討論:https://github.com/silexphp/Silex/issues/149

建議您定義的冗餘路徑,以斜槓隱:

$app->get('/hello', function() { 
    return 'Hello From HELLO!'; 
}); 

$app->get('/hello/', function() { 
    return 'Hello From HELLO!'; 
}); 

由於第二個參數是一個回調,這可能是:

$hello_handler = function() { 
    return 'Hello From HELLO!'; 
}; 
//or 
$hello_handler = array($object, 'handler_method'); 

$app->get('/hello', $hello_handler); 
$app->get('/hello/', $hello_handler); 

或者任何易於使用的方法,你想出。

原因是/index.html/index.html/不一樣。

+0

感謝您的快速回復@Michael。 – 2014-10-22 15:43:10

+0

我的朋友不是問題。 – Mike 2014-10-22 17:33:22