Phpbean Route-Forwarded PHP code _php Tutorial

Source: Internet
Author: User
First step: Getting Started
How do I use Phpbean for route forwarding? Here is a simple example of use.
First of all, index.php is a single entry for the program, mainly for routing and forwarding work. The program code for index.php 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 directory of the Controller
$router->dispatch ();//route forwarding processing
}catch (Exception $e) {
Echo $e;
}

?>

Where 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 test. (For the reasons why you choose action instead of using controller as control, you can look at my previous article to incrementally improve the performance of the framework)
Like app/actions/indexaction.php, write a test in there.
Class Indexaction extends Phpbean_action {

function Run () {
Echo ' Phpbean_index page! ';
}
}?>
Open IE input address http://localhost:8080/phpbean/index.php/index look at the results.

Step two: Get URL parameters
For example, I want to use the URL to pass parameters, how to implement in Phpbean? The pathinfo in Phpbean supports two ways to get data, one by keyword and one by order (default).
For the example above, add the parameter after 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 so that it corresponds to output 2. Note that the get URL parameter by keyword in Phpbean does not require matching pairing. For example, $this->_getparam (' 1 ', ' key ') returns 2, $this->_getparam (' 2 ', ' key ') will return 3.
It should be more convenient to get the URL parameters in order. For example, $this->_getparam (1) will return 1, $this->_getparam ($n) will return the $n parameter.

The third step, action multilevel directory
In general, the design of the program is module->controller->action, such as Blog->user->login. It is easy to map using the Phpbean action.

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

Now let's write blog->user->login this action. First build the loginaction.php below 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 common operation?" For example, all the actions under the Admin directory are ISAdmin () checked. "It is easy to use inheritance in Phpbean to implement it. The following example is implemented by Blog->admin.
First, add an admin directory where you add an action base class admin.php (note that if it's not an action, the file name is not named with ***action.php)

Abstract class _blog_admin extends Phpbean_action {

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

}
?>

Then, all the actions in the Admin directory inherit from the _blog_admin, not the phpbean_action, which enables the encapsulation of the common operation. For example, create a loginaction.php in the admin directory

Class Blog_admin_loginaction extends _blog_admin {

function Run () {
Echo ' Blog_admin_login ';
}

}
?>

http://www.bkjia.com/PHPjc/318528.html www.bkjia.com true http://www.bkjia.com/PHPjc/318528.html techarticle The first step: How to get started using Phpbean for routing and forwarding? Here is a simple example of use. First of all, index.php is a single entry for the program, mainly for routing and forwarding work. ...

  • Related Article

    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.