2016-09-07 27 views
1

我已經定義的路線是這樣的:如何在路由參數中發送URL?

$app->map(['GET', 'POST'],'/abc/[{url}]', function ($request, $response, $args) { 

    return $response; 
})->add(new CustomMiddleware()); 

其工作正常,當我通過一個URL,而不http://但給了我一個404 page not found -page與http://https://。我也嘗試使用url編碼的字符串,但給出相同的錯誤:

http://localhost/slim/public/index.php/abc/http%3A%2F%2Fstackoverflow.com 

The requested URL /slim/public/index.php/abc/http://stackoverflow.com was not found on this server. 

我正在使用Slim版本3.1。

+0

當我的回答可以幫到你,請把它標記爲正確的。 – jmattheis

+0

@jmattheis你的回答很好,但沒有達到我的問題。請通過這個問題,我想如何傳遞參數.. ??我已經意識到你的建議,但我想按照我的方式來做..反正感謝您的幫助。 – Ritesh

+0

是的,看看我的答案,爲什麼不能用未編碼的''/''來傳遞它,那麼編碼''/''怎麼可能呢?我現在不知道你想要什麼。 – jmattheis

回答

3

使用URL內的URL

當你將用斜槓的URL,然後路線沒有得到執行的原因則是未路由裏面definied在URL後附加路徑:

例如example.org/abc/test正常工作,但example.org/abc/http://x 只能使用此類路由定義/abc/{url}//{other}

使用經編碼的URL的URL內

阿帕奇塊中的URL以%5C\%2F所有請求/一個404 Not Found錯誤,這是因爲安全的原因。所以你不會從瘦身框架獲得404,而是從你的網絡服務器獲得404。所以你的代碼永遠不會被執行。

您可以通過設置AllowEncodedSlashes On來啓用此功能,其中包括您在apache的httpd.conf中。

我的建議來解決這個

添加URL作爲GET參數有有效有編碼不改變Apache的配置斜槓。

調用示例http://localhost/abc?url=http%3A%2F%2Fstackoverflow.com

$app->map(['GET', 'POST'], '/abc', function ($request, $response, $args) { 
    $getParam = $request->getQueryParams(); 
    $url= $getParam['url']; // is equal to http://stackoverflow.com 
});