PHP Controller Four

Source: Internet
Author: User
Simply put, the function of the controller is to accept the request. It uses the obtained method, where a function module is loaded through a URI to refresh or submit a presentation layer. The controller uses the $_get auto global variable to determine which module to load.

An example of a request that looks like this:

Http://example.com/index.php?module=login

This looks simple, but not in the process of implementation. Here are some of the argument parts that the controller can identify:

module defines which modules to use, such as the Users module
class defines which feature class to use, such as whether you want the user to login or logout
Event defines which specific event to use

Such a more complex example could explain the final request URL for each of the above argument:

Http://example.com/index.php?module=users&class=login

This request tells the Controller to load the users module, then the login class, and finally runs the Login::__default () default event because there is no specific event defined.

Here is the specific Code section:

<?php
/**
* index.php
*
* @author Joe Stump <joe@joestump.net>
* @copyright Joe Stump <joe@joestump.net>
* @license http://www.opensource.org/licenses/gpl-license.php
* @package Framework
*/

Require_once (' config.php ');

{{{__autoload ($class)
/**
* __autoload
*
* Autoload is-ran by PHP when it can ' t find a class It's trying to load.
* By naming our classes intelligently we should is able to the load most classes
* Dynamically.
*
* @author Joe Stump <joe@joestump.net>
* @param string $class class name we ' re trying to load
* @return void
* @package Framework
*/

function __autoload ($class)
{
$file = Str_replace (' _ ', '/', substr ($class, 2)). PHP ';
Require_once (Fr_base_path. ' /includes/'. $file);
}
// }}}

if (Isset ($_get[' module ')) {
$module = $_get[' module '];
if (Isset ($_get[' event '))) {
$event = $_get[' event '];
} else {
$event = ' __default ';
}

if (Isset ($_get[' class ')) {
$class = $_get[' class '];
} else {
$class = $module;
}

$classFile = Fr_base_path. ' /modules/'. $module. ' /'. $class. '. PHP ';
if (file_exists ($classFile)) {
Require_once ($classFile);
if (class_exists ($class)) {
try {
$instance = new $class ();
if (!fr_module::isvalid ($instance)) {
Die ("requested module was not a valid framework module!");
}

$instance->modulename = $module;
if ($instance->authenticate ()) {
try {
$result = $instance-> $event ();
if (! Pear::iserror ($result)) {
$presenter = Fr_presenter::factory ($instance->presenter, $instance);

if (! Pear::iserror ($presenter)) {
$presenter->display ();
} else {
Die ($presenter->getmessage ());
}
}
} catch (Exception $error) {
Die ($error->getmessage ());
}
} else {
Die ("does not have access to the requested page!");
}
} catch (Exception $error) {
Die ($error->getmessage ());
}
} else {
Die ("A valid module for your request is not found");
}
} else {
Die ("Could not find: $classFile");
}
} else {
Die ("A valid module is not specified");
}

? >

The following is a specific comment for the above code:

Load "config.php"

Defines the __autoload () function. This is a new function within the PHP5 that allows you to load classes dynamically.

If a argument is defined, then loading the relevant modules, classes, and specific events

The next step is some judgment and the wrong things.

Finally, it is loaded into the presentation layer after everything is correct.


Pretty URLs "friendly URLs"

If you feel uncomfortable with the request URL described in the example above, then use Mod_rewrite to implement the friendly URL. The following is the actual rewrite standard code written by the author to this framework:
Rewriteengine on
# The URI of this to whatever-want your homepage to be
REWR Iterule ^/$/index.php?module=welcome [L,QSA]
# changes/index.php?module=welcome to/welcome
RewriteCond%{DOCU Ment_root}/%{request_filename}!-d
Rewritecond%{document_root}/%{request_filename}!-f
RewriteRule ^/([^/]* ) $/index.php?module=$1 [L,QSA]
# changes/index.php?module=users&class=login to/users/login
RewriteCond% {document_root}/%{request_filename}!-d
Rewritecond%{document_root}/%{request_filename}!-f
RewriteRule ^/( [^/]*]/([^/]*) $/index.php?module=$1&class=$2 [L,QSA]
# Changes/index.php?module=users&class=login &event=foo
# to/users/login/foo.html
Rewritecond%{document_root}/%{request_filename}!-d
Rewritecond%{document_root}/%{request_filename}!-f

Rewriterule ^/([^/]*)/([^/]*)/([^/]*]. html$ \
/index.php?module=$1&class=$2&event=$3 [L,QSA]

Extending the Controller "expansion controllers"

One of the benefits of having a centralized controller is that when you add some functionality, you can immediately show it through the controller. Here are a few ideas to extend this controller to make the framework's overall capabilities more powerful:
You can use a new thing in PHP5. SoapServer to automatically detect whether a request is a soap

You can use the Controller to filter all automatic global variables such as $_get and $_post to prevent malicious HTML code, etc.

You can use the controller to instantly convert the presentation layer, such as from the default way to PDF

You can add the caching mechanism directly to the controller, and the benefit is that the whole application can use the cache to improve efficiency.

Of course, it is important to note that the functions you add to the controller will be reflected in the overall program. If you want to filter all the automatic global variables, but many application administrators need to use some HTML code, but it becomes a tricky thing (translator note: My idea is that you can add an IF condition statement, when loading a specific module does not apply filtering function).

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