Zend Framework动作助手Url用法详解
发布:smiling 来源: PHP粉丝网 添加日期:2021-07-13 14:45:31 浏览: 评论:0
这篇文章主要介绍了Zend Framework动作助手Url用法,结合实例形式分析了动作助手Url的功能,定义与相关使用技巧,需要的朋友可以参考下。
本文实例讲述了Zend Framework动作助手Url用法,分享给大家供大家参考,具体如下:
Url主要用于创建url;
- public function simple($action, $controller = null, $module = null, array $params = null)
- public function url($urlOptions = array(), $name = null, $reset = false, $encode = true)
- public function direct($action, $controller = null, $module = null, array $params = null)
- <?php
- class IndexController extends Zend_Controller_Action
- {
- public function init()
- {
- /* Initialize action controller here */
- }
- public function indexAction()
- {
- //$urlParser = $this->_helper->getHelper('UrlParser');
- //var_dump($urlParser->parse('https://www.jb51.net/article/80479.htm'));
- $url = $this->_helper->getHelper('Url');
- $action = 'actionname';
- $controller = 'controllername';
- $module = 'modulename';
- $params = array('param1'=>'中文参数');
- var_dump($url->simple($action, $controller, $module, $params));
- $urlOptions = array(
- 'action'=>$action,
- 'controller'=>$controller,
- 'module'=>$module,
- 'params'=>$params);
- var_dump($url->url($urlOptions));
- var_dump($url->direct($action, $controller, $module, $params));
- exit;
- }
- }
- www.phpfensi.com/helper_demo1/public/index
- string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
- string(101) "/helper_demo1/public/modulename/controllername/actionname/params/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
- string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
实现源码如下:
- /**
- * @see Zend_Controller_Action_Helper_Abstract
- */
- require_once 'Zend/Controller/Action/Helper/Abstract.php';
- /**
- * Helper for creating URLs for redirects and other tasks
- *
- * @uses Zend_Controller_Action_Helper_Abstract
- * @category Zend
- * @package Zend_Controller
- * @subpackage Zend_Controller_Action_Helper
- * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Abstract
- {
- /**
- * Create URL based on default route
- *
- * @param string $action
- * @param string $controller
- * @param string $module
- * @param array $params
- * @return string
- */
- public function simple($action, $controller = null, $module = null, array $params = null)
- {
- $request = $this->getRequest();
- if (null === $controller) {
- $controller = $request->getControllerName();
- }
- if (null === $module) {
- $module = $request->getModuleName();
- }
- $url = $controller . '/' . $action;
- if ($module != $this->getFrontController()->getDispatcher()->getDefaultModule()) {
- $url = $module . '/' . $url;
- }
- if ('' !== ($baseUrl = $this->getFrontController()->getBaseUrl())) {
- $url = $baseUrl . '/' . $url;
- }
- if (null !== $params) {
- $paramPairs = array();
- foreach ($params as $key => $value) {
- $paramPairs[] = urlencode($key) . '/' . urlencode($value);
- }
- $paramString = implode('/', $paramPairs);
- $url .= '/' . $paramString;
- }
- $url = '/' . ltrim($url, '/');
- return $url;
- }
- /**
- * Assembles a URL based on a given route
- *
- * This method will typically be used for more complex operations, as it
- * ties into the route objects registered with the router.
- *
- * @param array $urlOptions Options passed to the assemble method of the Route object.
- * @param mixed $name The name of a Route to use. If null it will use the current Route
- * @param boolean $reset
- * @param boolean $encode
- * @return string Url for the link href attribute.
- */
- public function url($urlOptions = array(), $name = null, $reset = false, $encode = true)
- {
- $router = $this->getFrontController()->getRouter();
- return $router->assemble($urlOptions, $name, $reset, $encode);
- }
- /**
- * Perform helper when called as $this->_helper->url() from an action controller
- *
- * Proxies to {@link simple()}
- *
- * @param string $action
- * @param string $controller
- * @param string $module
- * @param array $params
- * @return string
- */
- public function direct($action, $controller = null, $module = null, array $params = null)
- {
- return $this->simple($action, $controller, $module, $params);
- }
- }
Tags: Framework动作助手 Url
相关文章
- ·Codeigniter实现处理用户登录验证后的URL跳转(2021-02-18)
- ·CodeIgniter框架URL路由总结(2021-04-10)
- ·CI框架中site_url()和base_url()的区别(2021-05-08)
- ·Yii获取当前url和域名的方法(2021-05-27)
- ·CodeIgniter针对lighttpd服务器URL重写的方法(2021-05-28)
- ·YII使用url组件美化管理的方法(2021-07-02)
- ·CI(CodeIgniter)框架中URL特殊字符处理与SQL注入隐患分析(2021-11-11)
- ·Yii2框架自定义类统一处理url操作示例(2021-11-23)
- ·Yii2框架控制器、路由、Url生成操作示例(2021-11-23)
- ·laravel 获取当前url的别名方法(2022-01-01)
- ·关于laravel模板中生成URL的几种模式总结(2022-01-10)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)