回答
查找當前URL
當前的URL由Web服務器提供,並寫入$_SERVER
超級全局。運行這個小腳本<?php echo '<pre>'; print_r($_SERVER);
,通過您的服務器和根目錄找到您正在查找的值。在這個問題上
相關問題:
The PHP manual describes the nature of the available $_SERVER
values here。
獲取URL在嫩枝
之後,你有網址,你需要在樹枝模板實例調用當把它作爲一個模板變量。例如,你可以編碼。
$current_url = // figure out what the current url is
// pass the current URL as a variable to the template
echo $template->render(array('current_url' => $current_url));
要使用模板中的變量,請使用{{ variable_name }}
語法。
這裏我發現它使它與sliex框架通用。 我想我的解決方案並不完美,但它完成了工作。
在PHP代碼添加以下代碼:
$app = new Silex\Application();
// add the current url to the app object.
$app['current_url'] = $_SERVER['REQUEST_URI'];
然後在你的枝杈模板,你可以做
{{ app.current_url }}
讓我知道什麼是這種方法的botom線。
在Silex的以下作品和Symfony2的肯定,因爲它們共享請求類(我沒有,雖然測試是):
{{ app.request.getRequestUri() }}
'{{app.request.requestUri}}' – eightyfive 2014-07-31 04:52:12
更好,是的。 – neemzy 2014-08-01 08:43:27
已棄用,不應使用它。 https://github.com/silexphp/Silex/issues/1257#issuecomment-141058724 – Tieme 2016-07-06 15:09:10
轉到http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html
或:{{ app.request.getUri() }}
完全開放的。
記住最佳實踐,此時您應該使用Symfony\Component\HttpFoundation\RequestStack
。
請參閱http://symfony.com/blog/new-in-symfony-2-4-the-request-stack。
由於Symfony的2.4,最好的做法是從來沒有注入請求服務,而是改爲注入request_stack服務[...]
所以在Silex的應用程序也可能與實現:
app.request_stack.getCurrentRequest.getUri
- 1. 樹枝模板引擎的form_widget
- 2. 樹枝模板外部網址img
- 3. 如何在樹枝模板中獲取樹枝塊的來源?
- 4. 在cakephp中使用模板引擎樹枝?
- 5. 樹枝模板引擎函數或過濾器細化
- 6. 移動設備的樹枝模板引擎
- 7. 樹枝模板引擎和截短文本
- 8. 使用樹枝模板引擎訪問陣列
- 9. 可以用樹枝模板引擎組件
- 10. 樹枝模板引擎,使用靜態函數或變量
- 11. 如何獲取GAE模板中當前頁面的網址
- 12. Django模板:以另一種語言獲取當前網址
- 13. 樹枝模板 - 如何呈現HTML樹枝模板內
- 14. 跟蹤模板中的當前網址
- 15. 如何接取在樹枝模板
- 16. 樹枝模板提取子集
- 17. ASP.net MVC獲取當前視圖引擎
- 18. 的jQuery與樹枝模板
- 19. Symfony2樹枝加載模板
- 20. CKEditor在樹枝模板+ Symfony2
- 21. 映射和樹枝模板
- 22. 樹枝和相關模板
- 23. 包含在樹枝模板
- 24. 在一根樹枝模板
- 25. Angular2樹枝模板加載
- 26. 獲取當前網頁的網址
- 27. 基於DOM樹的JavaScript模板引擎
- 28. Django模板:將當前網址與{%網址xyz%}進行比較
- 29. 如何在樹枝模板引擎中使用php字符串函數
- 30. 從樹枝模板調用Medhod並獲取entityManager
爲什麼不傳遞當前URL作爲模板變量?或者你可以編寫一個輸出URL的模板標籤。 – 2012-02-18 07:09:19