Zend Framework动作助手Redirector用法实例详解,zendredirector_PHP教程
Zend Framework动作助手Redirector用法实例详解,zendredirector
本文实例讲述了Zend Framework动作助手Redirector用法。分享给大家供大家参考,具体如下:
Redirector 提供另一种实现方式,帮助程序重定向到内部或者外部页面;
转向器(Redirector)助手让你使用一个转向器对象帮助程序重定向到新的URL。与_redirect()方法相比,它具有多项优势。例如能够在转向器对象中预先配置整个站点的行为,或者使用与Zend_Controller_Action::_forward()相似的gotoSimple($action, $controller, $module, $params)接口。
转向器拥有影响重定向行为的大量方法:
setCode() 设置重定向过程中使用的HTTP响应码。
setExit() 在重定向后强制执行exit()方法。默认已设定。
setGotoSimple()设置默认的URL,当没有提供参数给gotoSimple()方法时转向该URL。可以使用类似Zend_Controller_Action::_forward()的API:setGotoSimple($action, $controller = null, $module = null, array $params = array());
setGotoRoute()设置基于一个注册路由器的URL。通过传入一个键/值数组和一个路由器名,它将根据路由器的类型和定义来组织URL。
setGotoUrl()设置默认的URL,当没有参数传入gotoUrl(),将使用该URL。接受单个URL字符串。
setPrependBase()在setGotoUrl()、gotoUrl()或者gotoUrlAndExit()指定的URL前面,加入请求对象的基地址(base URL)。
setUseAbsoluteUri()强制转向器在重定向时使用绝对的URI。当该选项设定后,将使用$_SERVER['HTTP_HOST']、 $_SERVER['SERVER_PORT']和 $_SERVER['HTTPS']以及重定向方法指定的URL,来形成一个完整的URI。该选项目前默认关闭,将来的版本可能会默认开启。
此外,转向器中还有大量方法来执行实际的重定向。
gotoSimple()使用setGotoSimple()(类似_forward()的API)来构建URL并执行重定向。
gotoRoute()使用setGotoRoute()(路由组装route-assembly)来构建URL并执行重定向。
gotoUrl()使用setGotoUrl() URL字符串)来构造URL并执行重定向。
最后,你可以在任何时刻使用getRedirectUrl()确定当前的重定向URL。
基础用例
Example #5 设定选项
这个例子改变了几个选项,包括设定重定向时使用的HTTP状态码为303,重定向时不默认退出,以及定义了默认的URL供重定向使用。
class SomeController extends Zend_Controller_Action { /** * Redirector - defined for code completion * * @var Zend_Controller_Action_Helper_Redirector */ protected $_redirector = null; public function init() { $this->_redirector = $this->_helper->getHelper('Redirector'); // Set the default options for the redirector // Since the object is registered in the helper broker, these // become relevant for all actions from this point forward $this->_redirector->setCode(303) ->setExit(false) ->setGotoSimple("this-action", "some-controller"); } public function myAction() { /* do some stuff */ // Redirect to a previously registered URL, and force an exit // to occur when done: $this->_redirector->redirectAndExit(); return; // never reached } }
Example #6 使用默认设定
这个例子假定使用默认设定,也就意味着任何重定向将导致立即退出。
// ALTERNATIVE EXAMPLE class AlternativeController extends Zend_Controller_Action { /** * Redirector - defined for code completion * * @var Zend_Controller_Action_Helper_Redirector */ protected $_redirector = null; public function init() { $this->_redirector = $this->_helper->getHelper('Redirector'); } public function myAction() { /* do some stuff */ $this->_redirector ->gotoUrl('/my-controller/my-action/param1/test/param2/test2'); return; // never reached since default is to goto and exit } }
Example #7 使用goto()的_forward()API
gotoSimple()'s API 模拟了Zend_Controller_Action::_forward()。主要的不同在于它通过传入的参数构造URL,使用默认路由器的默认格式:module/:controller/:action/*。然后重定向而不是继续动作链循环。
class ForwardController extends Zend_Controller_Action { /** * Redirector - defined for code completion * * @var Zend_Controller_Action_Helper_Redirector */ protected $_redirector = null; public function init() { $this->_redirector = $this->_helper->getHelper('Redirector'); } public function myAction() { /* do some stuff */ // Redirect to 'my-action' of 'my-controller' in the current // module, using the params param1 => test and param2 => test2 $this->_redirector->gotoSimple('my-action', 'my-controller', null, array('param1' => 'test', 'param2' => 'test2' ) ); } }
Example #8 通过gotoRoute()使用路由组装(route assembly)
下面的例子使用了路由器的assemble()方法,基于传入参数的关联数组来创建URL。假定下面的路由已经注册:
$route = new Zend_Controller_Router_Route( 'blog/:year/:month/:day/:id', array('controller' => 'archive', 'module' => 'blog', 'action' => 'view') ); $router->addRoute('blogArchive', $route);
给定一个数组,其中年份为2006,月份为4,日期为24,id为42,据此可以组装URL/blog/2006/4/24/42。
class BlogAdminController extends Zend_Controller_Action { /** * Redirector - defined for code completion * * @var Zend_Controller_Action_Helper_Redirector */ protected $_redirector = null; public function init() { $this->_redirector = $this->_helper->getHelper('Redirector'); } public function returnAction() { /* do some stuff */ // Redirect to blog archive. Builds the following URL: // /blog/2006/4/24/42 $this->_redirector->gotoRoute( array('year' => 2006, 'month' => 4, 'day' => 24, 'id' => 42), 'blogArchive' ); } }
Zend_Controller_Action_Helper_Redirector的源码。
通过源代码不难看出实现方法,以及常见的使用方法。
<?php /** * @see Zend_Controller_Action_Helper_Abstract */ require_once 'Zend/Controller/Action/Helper/Abstract.php'; /** * @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_Redirector extends Zend_Controller_Action_Helper_Abstract { /** * HTTP status code for redirects * @var int */ protected $_code = 302; /** * Whether or not calls to _redirect() should exit script execution * @var boolean */ protected $_exit = true; /** * Whether or not _redirect() should attempt to prepend the base URL to the * passed URL (if it's a relative URL) * @var boolean */ protected $_prependBase = true; /** * Url to which to redirect * @var string */ protected $_redirectUrl = null; /** * Whether or not to use an absolute URI when redirecting * @var boolean */ protected $_useAbsoluteUri = false; /** * Whether or not to close the session before exiting * @var boolean */ protected $_closeSessionOnExit = true; /** * Retrieve HTTP status code to emit on {@link _redirect()} call * * @return int */ public function getCode() { return $this->_code; } /** * Validate HTTP status redirect code * * @param int $code * @throws Zend_Controller_Action_Exception on invalid HTTP status code * @return true */ protected function _checkCode($code) { $code = (int)$code; if ((300 > $code) || (307 < $code) || (304 == $code) || (306 == $code)) { require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception('Invalid redirect HTTP status code (' . $code . ')'); } return true; } /** * Retrieve HTTP status code for {@link _redirect()} behaviour * * @param int $code * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface */ public function setCode($code) { $this->_checkCode($code); $this->_code = $code; return $this; } /** * Retrieve flag for whether or not {@link _redirect()} will exit when finished. * * @return boolean */ public function getExit() { return $this->_exit; } /** * Retrieve exit flag for {@link _redirect()} behaviour * * @param boolean $flag * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface */ public function setExit($flag) { $this->_exit = ($flag) ? true : false; return $this; } /** * Retrieve flag for whether or not {@link _redirect()} will prepend the * base URL on relative URLs * * @return boolean */ public function getPrependBase() { return $this->_prependBase; } /** * Retrieve 'prepend base' flag for {@link _redirect()} behaviour * * @param boolean $flag * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface */ public function setPrependBase($flag) { $this->_prependBase = ($flag) ? true : false; return $this; } /** * Retrieve flag for whether or not {@link redirectAndExit()} shall close the session before * exiting. * * @return boolean */ public function getCloseSessionOnExit() { return $this->_closeSessionOnExit; } /** * Set flag for whether or not {@link redirectAndExit()} shall close the session before exiting. * * @param boolean $flag * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface */ public function setCloseSessionOnExit($flag) { $this->_closeSessionOnExit = ($flag) ? true : false; return $this; } /** * Return use absolute URI flag * * @return boolean */ public function getUseAbsoluteUri() { return $this->_useAbsoluteUri; } /** * Set use absolute URI flag * * @param boolean $flag * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface */ public function setUseAbsoluteUri($flag = true) { $this->_useAbsoluteUri = ($flag) ? true : false; return $this; } /** * Set redirect in response object * * @return void */ protected function _redirect($url) { if ($this->getUseAbsoluteUri() && !preg_match('#^(https?|ftp)://#', $url)) { $host = (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:''); $proto = (isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=="off") ? 'https' : 'http'; $port = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80); $uri = $proto . '://' . $host; if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) { // do not append if HTTP_HOST already contains port if (strrchr($host, ':') === false) { $uri .= ':' . $port; } } $url = $uri . '/' . ltrim($url, '/'); } $this->_redirectUrl = $url; $this->getResponse()->setRedirect($url, $this->getCode()); } /** * Retrieve currently set URL for redirect * * @return string */ public function getRedirectUrl() { return $this->_redirectUrl; } /** * Determine if the baseUrl should be prepended, and prepend if necessary * * @param string $url * @return string */ protected function _prependBase($url) { if ($this->getPrependBase()) { $request = $this->getRequest(); if ($request instanceof Zend_Controller_Request_Http) { $base = rtrim($request->getBaseUrl(), '/'); if (!empty($base) && ('/' != $base)) { $url = $base . '/' . ltrim($url, '/'); } else { $url = '/' . ltrim($url, '/'); } } } return $url; } /** * Set a redirect URL of the form /module/controller/action/params * * @param string $action * @param string $controller * @param string $module * @param array $params * @return void */ public function setGotoSimple($action, $controller = null, $module = null, array $params = array()) { $dispatcher = $this->getFrontController()->getDispatcher(); $request = $this->getRequest(); $curModule = $request->getModuleName(); $useDefaultController = false; if (null === $controller && null !== $module) { $useDefaultController = true; } if (null === $module) { $module = $curModule; } if ($module == $dispatcher->getDefaultModule()) { $module = ''; } if (null === $controller && !$useDefaultController) { $controller = $request->getControllerName(); if (empty($controller)) { $controller = $dispatcher->getDefaultControllerName(); } } $params[$request->getModuleKey()] = $module; $params[$request->getControllerKey()] = $controller; $params[$request->getActionKey()] = $action; $router = $this->getFrontController()->getRouter(); $url = $router->assemble($params, 'default', true); $this->_redirect($url); } /** * Build a URL based on a route * * @param array $urlOptions * @param string $name Route name * @param boolean $reset * @param boolean $encode * @return void */ public function setGotoRoute(array $urlOptions = array(), $name = null, $reset = false, $encode = true) { $router = $this->getFrontController()->getRouter(); $url = $router->assemble($urlOptions, $name, $reset, $encode); $this->_redirect($url); } /** * Set a redirect URL string * * By default, emits a 302 HTTP status header, prepends base URL as defined * in request object if url is relative, and halts script execution by * calling exit(). * * $options is an optional associative array that can be used to control * redirect behaviour. The available option keys are: * - exit: boolean flag indicating whether or not to halt script execution when done * - prependBase: boolean flag indicating whether or not to prepend the base URL when a relative URL is provided * - code: integer HTTP status code to use with redirect. Should be between 300 and 307. * * _redirect() sets the Location header in the response object. If you set * the exit flag to false, you can override this header later in code * execution. * * If the exit flag is true (true by default), _redirect() will write and * close the current session, if any. * * @param string $url * @param array $options * @return void */ public function setGotoUrl($url, array $options = array()) { // prevent header injections $url = str_replace(array("\n", "\r"), '', $url); if (null !== $options) { if (isset($options['exit'])) { $this->setExit(($options['exit']) ? true : false); } if (isset($options['prependBase'])) { $this->setPrependBase(($options['prependBase']) ? true : false); } if (isset($options['code'])) { $this->setCode($options['code']); } } // If relative URL, decide if we should prepend base URL if (!preg_match('|^[a-z]+://|', $url)) { $url = $this->_prependBase($url); } $this->_redirect($url); } /** * Perform a redirect to an action/controller/module with params * * @param string $action * @param string $controller * @param string $module * @param array $params * @return void */ public function gotoSimple($action, $controller = null, $module = null, array $params = array()) { $this->setGotoSimple($action, $controller, $module, $params); if ($this->getExit()) { $this->redirectAndExit(); } } /** * Perform a redirect to an action/controller/module with params, forcing an immdiate exit * * @param mixed $action * @param mixed $controller * @param mixed $module * @param array $params * @return void */ public function gotoSimpleAndExit($action, $controller = null, $module = null, array $params = array()) { $this->setGotoSimple($action, $controller, $module, $params); $this->redirectAndExit(); } /** * Redirect to a route-based URL * * Uses route's assemble method tobuild the URL; route is specified by $name; * default route is used if none provided. * * @param array $urlOptions Array of key/value pairs used to assemble URL * @param string $name * @param boolean $reset * @param boolean $encode * @return void */ public function gotoRoute(array $urlOptions = array(), $name = null, $reset = false, $encode = true) { $this->setGotoRoute($urlOptions, $name, $reset, $encode); if ($this->getExit()) { $this->redirectAndExit(); } } /** * Redirect to a route-based URL, and immediately exit * * Uses route's assemble method tobuild the URL; route is specified by $name; * default route is used if none provided. * * @param array $urlOptions Array of key/value pairs used to assemble URL * @param string $name * @param boolean $reset * @return void */ public function gotoRouteAndExit(array $urlOptions = array(), $name = null, $reset = false) { $this->setGotoRoute($urlOptions, $name, $reset); $this->redirectAndExit(); } /** * Perform a redirect to a url * * @param string $url * @param array $options * @return void */ public function gotoUrl($url, array $options = array()) { $this->setGotoUrl($url, $options); if ($this->getExit()) { $this->redirectAndExit(); } } /** * Set a URL string for a redirect, perform redirect, and immediately exit * * @param string $url * @param array $options * @return void */ public function gotoUrlAndExit($url, array $options = array()) { $this->setGotoUrl($url, $options); $this->redirectAndExit(); } /** * exit(): Perform exit for redirector * * @return void */ public function redirectAndExit() { if ($this->getCloseSessionOnExit()) { // Close session, if started if (class_exists('Zend_Session', false) && Zend_Session::isStarted()) { Zend_Session::writeClose(); } elseif (isset($_SESSION)) { session_write_close(); } } $this->getResponse()->sendHeaders(); exit(); } /** * direct(): Perform helper when called as * $this->_helper->redirector($action, $controller, $module, $params) * * @param string $action * @param string $controller * @param string $module * @param array $params * @return void */ public function direct($action, $controller = null, $module = null, array $params = array()) { $this->gotoSimple($action, $controller, $module, $params); } /** * Overloading * * Overloading for old 'goto', 'setGoto', and 'gotoAndExit' methods * * @param string $method * @param array $args * @return mixed * @throws Zend_Controller_Action_Exception for invalid methods */ public function __call($method, $args) { $method = strtolower($method); if ('goto' == $method) { return call_user_func_array(array($this, 'gotoSimple'), $args); } if ('setgoto' == $method) { return call_user_func_array(array($this, 'setGotoSimple'), $args); } if ('gotoandexit' == $method) { return call_user_func_array(array($this, 'gotoSimpleAndExit'), $args); } require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception(sprintf('Invalid method "%s" called on redirector', $method)); } }
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- Zend Framework教程之MVC框架的Controller用法分析
- Zend Framework教程之路由功能Zend_Controller_Router详解
- Zend Framework教程之Zend_Controller_Plugin插件用法详解
- Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
- Zend Framework教程之请求对象的封装Zend_Controller_Request实例详解
- Zend Framework教程之动作的基类Zend_Controller_Action详解
- Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解
- Zend Framework教程之前端控制器Zend_Controller_Front用法详解
- Zend Framework动作助手Url用法详解
- Zend Framework动作助手Json用法实例分析
- Zend Framework动作助手FlashMessenger用法详解
- Zend Framework教程之Resource Autoloading用法实例

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











.NET Framework 4는 개발자와 최종 사용자가 Windows에서 최신 버전의 애플리케이션을 실행하는 데 필요합니다. 그러나 .NET Framework 4를 다운로드하고 설치하는 동안 많은 사용자가 설치 프로그램이 중간에 중지되고 "오류 코드 0x800c0006으로 인해 다운로드에 실패했기 때문에 .NET Framework 4가 설치되지 않았습니다"라는 오류 메시지가 표시된다고 불평했습니다. 장치에 .NETFramework4를 설치하는 동안에도 이 문제가 발생한다면 올바른 위치에 있는 것입니다.

Windows 11 또는 Windows 10 PC에 업그레이드 또는 업데이트 문제가 있을 때마다 일반적으로 실패의 실제 원인을 나타내는 오류 코드가 표시됩니다. 그러나 오류 코드가 표시되지 않고 업그레이드나 업데이트가 실패하면 혼란이 발생할 수 있습니다. 편리한 오류 코드를 사용하면 문제가 어디에 있는지 정확히 알 수 있으므로 문제를 해결할 수 있습니다. 하지만 오류 코드가 나타나지 않기 때문에 문제를 식별하고 해결하기가 어렵습니다. 단순히 오류의 원인을 찾는 데 많은 시간이 걸립니다. 이 경우 오류의 실제 원인을 쉽게 식별하는 데 도움이 되는 Microsoft에서 제공하는 SetupDiag라는 전용 도구를 사용해 볼 수 있습니다.
![SCNotification이 작동을 멈췄습니다. [수정을 위한 5단계]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows 사용자는 컴퓨터를 시작할 때마다 SCNotification이 작동을 중지했습니다. 오류가 발생할 수 있습니다. SCNotification.exe는 권한 오류 및 네트워크 오류로 인해 PC를 시작할 때마다 충돌이 발생하는 Microsoft 시스템 알림 파일입니다. 이 오류는 문제가 있는 이벤트 이름으로도 알려져 있습니다. 따라서 이를 SCNotification의 작동이 중지된 것으로 표시되지 않고 버그 clr20r3으로 표시될 수 있습니다. 이 기사에서는 SCNotification이 작동을 중지하여 다시 귀찮게 하지 않도록 수정하기 위해 취해야 할 모든 단계를 살펴보겠습니다. SCNotification.e는 무엇입니까

Microsoft.NET 버전 4.5.2, 4.6 또는 4.6.1을 설치한 Microsoft Windows 사용자가 Microsoft에서 향후 제품 업데이트를 통해 프레임워크를 지원하도록 하려면 최신 버전의 Microsoft Framework를 설치해야 합니다. Microsoft에 따르면 세 가지 프레임워크 모두 2022년 4월 26일에 지원이 중단됩니다. 지원 날짜가 종료되면 해당 제품은 "보안 수정 또는 기술 지원"을 받을 수 없습니다. 대부분의 가정용 장치는 Windows 업데이트를 통해 최신 상태로 유지됩니다. 이러한 장치에는 .NET Framework 4.8과 같은 최신 버전의 프레임워크가 이미 설치되어 있습니다. 자동으로 업데이트되지 않는 장치는

Windows 11용 KB5012643을 설치한 사용자에게 영향을 미치는 새로운 안전 모드 버그에 대해 이야기한 지 일주일이 지났습니다. 이 성가신 문제는 Microsoft가 출시일에 게시한 알려진 문제 목록에 나타나지 않아 모두를 놀라게 했습니다. 글쎄, 상황이 더 이상 악화될 수 없다고 생각했을 때 Microsoft는 이 누적 업데이트를 설치한 사용자에게 또 다른 폭탄을 떨어뜨렸습니다. Windows 11 빌드 22000.652로 인해 더 많은 문제 발생 따라서 기술 회사는 Windows 11 사용자에게 일부 .NET Framework 3.5 응용 프로그램을 시작하고 사용하는 데 문제가 발생할 수 있다고 경고합니다. 익숙한 것 같나요? 하지만 놀라지 마세요.

Zend Framework에서 권한 제어를 위해 ACL(AccessControlList)을 사용하는 방법 소개: 웹 애플리케이션에서 권한 제어는 중요한 기능입니다. 이는 사용자가 액세스 권한이 있는 페이지와 기능에만 액세스할 수 있도록 하고 무단 액세스를 방지합니다. Zend 프레임워크는 ACL(AccessControlList) 구성 요소를 사용하여 권한 제어를 구현하는 편리한 방법을 제공합니다. 이 기사에서는 Zend Framework에서 ACL을 사용하는 방법을 소개합니다.

PHP 구현 프레임워크: ZendFramework 입문 튜토리얼 ZendFramework는 PHP에서 개발하고 현재 ZendTechnologies에서 유지 관리하는 오픈 소스 웹 사이트 프레임워크입니다. ZendFramework는 MVC 디자인 패턴을 채택하고 Web2.0 애플리케이션 및 Web Serve 구현을 지원하기 위한 재사용 가능한 코드 라이브러리 시리즈를 제공합니다. ZendFramework는 PHP 개발자들에게 매우 인기 있고 존경받고 있으며 다양한 기능을 갖추고 있습니다.

12월 9일 뉴스에 따르면 Cooler Master는 최근 타이페이 컴퓨트 쇼(Taipei Compute Show)의 시연 행사에서 노트북 모듈 솔루션 제공업체인 Framework와 협력하여 미니 섀시 키트를 시연했습니다. 프레임워크 노트북에서. 현재 이 제품은 시중에 판매되기 시작했으며 가격은 39달러로 현재 환율로 약 279위안(한화 약 279위안)에 해당한다. 이 섀시 키트의 모델 번호는 "frameWORKMAINBOARDCASE"로 명명됩니다. 디자인 측면에서는 297x133x15mm 크기로 최고의 컴팩트함과 실용성을 구현합니다. 원래 디자인은 프레임워크 노트북에 원활하게 연결할 수 있도록 하는 것입니다.
