Example of adding a URL extension automatically in the Yaf framework

Source: Internet
Author: User
Tags strlen

Like Codeigniter, Yaf also sets several hook functions to take over or change the program direction as appropriate.

1 RouterStartup Triggered before routing This is the first of the seven events, but some global work should be completed in Bootstrap.
2 RouterShutdown Triggered after route ends The route must be completed correctly. Otherwise, this event will not be triggered.
3 DispatchLoopStartup Triggered before the distribution cycle starts
4 PreDispatch Triggered before distribution If a forward occurs during request processing, this event will be triggered multiple times.
5 PostDispatch Triggered after distribution At this time, the action has been executed and the view has been rendered. Similar to preDispatch, this event may trigger multiple times.
6 DispatchLoopShutdown Triggered after the distribution cycle ends This indicates that all the business logic has been completed, but the response has not been sent.


You can see from the Hook definition that routerStartup is the hook before route initialization. Therefore, you can control the REQUEST_URI suffix in routerStartup and intercept it with a specific suffix.
Example:

# Application \ plugins \ System. php # Yaf_Registry: get ('config')-> application-> url_suffix is the suffix defined in the configuration file, for example :. html class SystemPlugin extends Yaf_Plugin_Abstract {public function routerStartup (Yaf_Request_Abstract $ request, Yaf_Response_Abstract $ response) {if (Yaf_Registry: get ('config')-> application-> url_suffix) {if (strtolower (substr ($ _ SERVER ['request _ URI '],-strlen (Yaf_Registry: get ('config')-> application-> url_suffix ))) = strtolower (Yaf_Registry: get ('config')-> application-> url_suffix )) {$ request-> setRequestUri (substr ($ _ SERVER ['request _ URI '], 0,-strlen (Yaf_Registry: get ('config ') -> application-> url_suffix )));}}}}

Here, the URL is obtained through REQUEST_URI. Other methods are the same. Then, use the uniform URL creation method on the page, and add the suffix when generating the URL.

Implementation of URL Rewrite. htaccess

<IfModule mod_rewrite.c> RewriteEngine on # if a directory or a file exists, use it directlyRewriteCond % {REQUEST_FILENAME }! -FRewriteCond % {REQUEST_FILENAME }! -D # otherwise forward it to index. phpRewriteRule. index. php </IfModule>

SAE

-Rewrite: if (! Is_file ()&&! Is_dir () & path ~ "^/(. *)") Goto "index. php/$1"

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.