Php code for Phpbean route forwarding

Source: Internet
Author: User

Step 1: getting started
How can I use phpbean for route forwarding? The following is a simple example.
First, index. php is a single entry to the program, mainly for routing and forwarding. The program code of Index. php is as follows:

<?
Include ('startup. php'); // load the startup File
Try {
$ Router = new Phpbean_Router (); // initialize the router
$ Router-> set_path (SITE_PATH. '/app/actions'); // set the Controller directory
$ Router-> dispatch (); // route forwarding
} Catch (Exception $ e ){
Echo $ e;
}

?>

The Startup File is as follows:

<?
Define ('site _ path', str_replace ('\', '/', dirname (_ FILE __)));
Set_include_path (get_include_path (). PATH_SEPARATOR.SITE_PATH. '/libs /');
Function _ autoload ($ filename ){
$ Filepath = SITE_PATH. '/libs/'. str_replace ('_', '/', $ filename). '. php ';
If (is_file ($ filepath )){
Include ($ filepath );
}
}?>

Then, let's write an action to test it. (For more information about how to choose action instead of controller, see my previous article to gradually improve the performance of the Framework)
For example, in app/actions/indexAction. php, write a test
<?
Class indexAction extends Phpbean_Action {

Function run (){
Echo 'phpbean _ index page! ';
}
}?>
Open the IE input address http: // localhost: 8080/phpbean/index. php/index and check the result.

Step 2: Obtain url parameters
For example, if I want to use url to pass parameters, how can I implement it in PHPbean? The pathinfo in phpbean supports two data retrieval methods, one is by keyword, and the other is by Order (default ).
In the preceding example, the address http: // localhost: 8080/phpbean/index. php/index is followed by the parameter http: // localhost: 8080/phpbean/index. php/index/1/2/3/4/
Then, use the keyword to obtain the url parameter and use $ this-> _ getParam ('1', 'key '). For example, if echo $ this-> _ getParam ('1', 'key') is added to the run method of indexAction, 2 is output. Note: Retrieving url parameters by keyword in phpbean does not require matching. For example, $ this-> _ getParam ('1', 'key') returns 2, $ this-> _ getParam ('2', 'key') returns 3.
It is easier to obtain url parameters in order. For example, $ this-> _ getParam (1) will return 1, $ this-> _ getParam ($ n) will return the $ n parameter.

Step 3: multi-level Action directory
Generally, the program is designed to be module-> controller-> action, for example, blog-> user-> login. The phpbean action can be easily mapped.

First, add the blog folder under app/actions, and then add the user folder under app/actions/blog.

Now let's write the action "blog"> "user"> "login. First, create loginAction. php under app/actions/blog/user. Write in

<?
Class blog_user_loginAction extends Phpbean_Action {

Function run (){
Echo 'blog _ user_login ';
}

}
?>

Then test http: // localhost: 8080/phpbean/index. php/blog/user/login/. Is it successful?

Then, "how to add some public actions for actions? For example, all actions under the admin directory must be checked by isadmin (). How can this problem be achieved ?" In phpbean, inheritance can be easily implemented. The following uses blog-> admin as an example.
First, add an admin directory and add an action base class admin under the directory. php (Note: if it is not an action, the file name should not use *** Action. php)

<?
Abstract class _ blog_admin extends Phpbean_Action {

Function _ construct ($ params ){
Parent: :__ construct ($ params );
Echo 'admin check! ';
}

}
?>

Then, all actions under the admin directory are inherited from _ blog_admin, rather than Phpbean_Action, which encapsulates public operations. For example, create a loginAction. php file in the admin directory.

<?
Class blog_admin_loginAction extends _ blog_admin {

Function run (){
Echo 'blog _ admin_login ';
}

}
?>

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.