Phpbean Routing-forwarding PHP code _php template

Source: Internet
Author: User
Tags php code
First step: Getting Started
How do I use Phpbean to route forwarding? Here is a simple example of use.
First of all, index.php is a simple introduction to the program, the main route forwarding work. Index.php's program code is as follows:

?
Include (' startup.php ');/load Boot file
try{
$router =new phpbean_router ();//Initialize Router
$router->set_path (site_path. ') /app/actions ');//Set the controller's directory
$router->dispatch ();//Routing forwarding processing
}catch (Exception $e) {
Echo $e;
}

?>

The startup files are 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 test. (For reasons of choosing action instead of using controller as control, you can see my previous article gradually improve the performance of the framework)
Like app/actions/indexaction.php, write a test inside.
?
Class Indexaction extends Phpbean_action {

function Run () {
Echo ' Phpbean_index page! ';
}
}?>
Open IE enter address Http://localhost:8080/phpbean/index.php/index to see the results.

Step two: Get the URL parameter
For example, I want to use the URL to pass parameters, how to implement in the Phpbean? The pathinfo in Phpbean supports two ways to get data, one by keyword and one in order (default).
In the example above, add the parameters after the address Http://localhost:8080/phpbean/index.php/index http://localhost:8080/phpbean/index.php/index/1/2/3/4/
Then, use the keyword to get the URL parameter, using $this->_getparam (' 1 ', ' key '). For example, add the Echo $this->_getparam (' 1 ', ' key ') to the Indexaction run method, and then output 2. Note that obtaining a URL parameter by keyword in Phpbean does not require a matching pairing. For example, $this->_getparam (' 1 ', ' key ') returns 2, $this->_getparam (' 2 ', ' key ') returns 3.
It should be more convenient to get the URL parameters in order. For example, $this->_getparam (1) returns 1, $this->_getparam ($n) returns the $n argument.

Step three, action multilevel directory
In general, the design of the program is module->controller->action, such as Blog->user->login. Then using the Phpbean action can be very handy for mapping.

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

Now we're going to write blog->user->login this action. First set up the loginaction.php under the app/actions/blog/user/. Inside write

?
Class Blog_user_loginaction extends Phpbean_action {

function Run () {
Echo ' Blog_user_login ';
}

}
?>

Then test the next http://localhost:8080/phpbean/index.php/blog/user/login/, is it successful?

So, "How do you add some action to the public action?" For example, all the action under the Admin directory should be ISAdmin () check, how to achieve it? "You can use inheritance in Phpbean to implement it easily. The following is an example of blog->admin.
First, add an admin directory, add an action base class to the directory admin.php (note, if not the Action, then the file name should not use ***action.php to name)

?
Abstract class _blog_admin extends Phpbean_action {

function __construct ($params) {
Parent::__construct ($params);
Echo ' admin check! ';
}

}
?>

Then, all actions under the Admin directory inherit from the _blog_admin rather than the phpbean_action, thus implementing the encapsulation of the common operation. For example, under the Admin directory to create a loginaction.php

?
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.