PHP MVC Framework Core Class

Source: Internet
Author: User
Tags autoload

now we present several examples of core frameworks: Create a Framework.class.php file under Framework/core. Write the following code:

framework/core/framework.class.php

Class Framework {

public static function run () {

echo "Run ()";

}

Brother Lian Education in this demo Create a static method run (), now let's test it through the portal file index.php:

<?php

Require "framework/core/framework.class.php";

Framework::run ();

You can visit index.php in your browser to see the results. Usually this static method is named run () or bootstrap (). In this approach, we have to do 3 of the most important things:

Class Framework {

public static function run () {

echo "Run ()";

Self::init ();

Self::autoload ();

Self::d ispatch ();

}

private static function init () {

}

private static function AutoLoad () {

}

private static function dispatch () {

}

}

Initialization

Init () Method:

Initialization

private static function init () {

Define Path Constants

Define ("DS", directory_separator);

Define ("ROOT", GETCWD (). DS);

Define ("App_path", ROOT. ' Application '. DS);

Define ("Framework_path", ROOT. "Framework". DS);

Define ("Public_path", ROOT. "Public". DS);

Define ("Config_path", App_path. "Config". DS);

Define ("Controller_path", App_path. "Controllers". DS);

Define ("Model_path", App_path. "Models". DS);

Define ("View_path", App_path. "Views". DS);

Define ("Core_path", Framework_path. "Core". DS);

Define (' Db_path ', Framework_path. "Database". DS);

Define ("Lib_path", Framework_path. "Libraries". DS);

Define ("Helper_path", Framework_path. "Helpers". DS);

Define ("Upload_path", Public_path. "Uploads". DS);

Define platform, Controller, action, for example:

Index.php?p=admin&c=goods&a=add

Define ("PLATFORM", Isset ($_request[' P '))? $_request[' P ']: ' home ');

Define ("CONTROLLER", Isset ($_request[' C '))? $_request[' C ']: ' Index ');

Define ("ACTION", Isset ($_request[' a ')), $_request[' a ']: ' index ');

Define ("Curr_controller_path", Controller_path. PLATFORM. DS);

Define ("Curr_view_path", View_path. PLATFORM. DS);

Load Core Classes

Require Core_path. "Controller.class.php";

Require Core_path. "Loader.class.php";

Require Db_path. "Mysql.class.php";

Require Core_path. "Model.class.php";

Load configuration file

$GLOBALS [' config '] = include config_path. "Config.php";

Start session

Session_Start ();

}

In the comments you can see the purpose of each step.

Auto Load

In the project, we don't want to manually go to include or require when we want to use a class in the script, which is why the PHP MVC framework has the ability to load automatically. For example, in Symfony, if you want to load a class under LIB, it will be introduced automatically. It's amazing, isn't it? Now let's add the auto-load feature to our own framework.

Here we are going to use the self-contained function in PHP Spl_autoload_register:

Autoloading

private static function AutoLoad () {

Spl_autoload_register (Array (__class__, ' Load '));

}

Define a custom Load method

private static function Load ($classname) {

Here simply autoload app ' s controller and model classes

if (substr ($classname, -10) = = "Controller") {

Controller

Require_once Curr_controller_path. "$classname. class.php";

} elseif (substr ($classname,-5) = = "Model") {

Model

Require_once Model_path. "$classname. class.php";

}

}

Each frame has its own naming convention, and ours is no exception. For a controller class, it needs to be named like XxxController.class.php, and for a model class, it needs to be named xxModel.class.php. Why do you need to follow its naming conventions when using a framework? Automatic loading is one reason.

Routing/Distribution

Routing and dispatching

private static function dispatch () {

Instantiate the Controller class and call its action method

$controller _name = controller. "Controller";

$action _name = action. "Action";

$controller = new $controller _name;

$controller, $action _name ();

}

In this step, index.php will distribute the request to the corresponding Controller::aciton () method.

Base Controller class

Usually there is a base controller in the core class of the framework. In Symfony, it is called sfaction; in iOS, it is called Uiviewcontroller. Here we name the controller and build the Controller.class.php under Framework/core.

<?php

Base Controller

Class controller{

Base Controller has a property called $loader, it's an instance of loader class (introduced later)

protected $loader;

Public Function __construct () {

$this->loader = new loader ();

}

Public function Redirect ($url, $message, $wait = 0) {

if ($wait = = 0) {

Header ("Location: $url");

} else {

Include Curr_view_path. "Message.html";

}

Exit

}

}

The base controller has a variable $loader, which is an instantiation of the loader class (described later). To be exact, $this->loader is a variable that points to the load class being instantiated. I don't talk too much here, but it's really a very critical concept. I've met some PHP developers who believe that after this statement:

$this->loader = new loader ();

$this->load is an object. No, it's just a reference. This is started in Java, and is called a pointer in C + + and objective C before Java. The reference is a encapsulated pointer type. For example, in iOS (O-C), we created an object:

UIButton *btn = [UIButton alloc] init];

Load class

In framework.class.php, we have encapsulated the automatic loading of the controller and model of the application. But how do you automatically load classes in the framework directory? Now we can create a new loader class that loads the classes and functions in the framework directory. When we load the framework class, we just need to call the method in the loader class.

Class loader{

Load Library Classes

Public Function library ($lib) {

Include Lib_path. "$lib. class.php";

}

Loader helper functions. naming conversion is xxx_helper.php;

Public Function Helper ($helper) {

Include Helper_path. "{$helper}_helper.php";

}

}


This article is from the "11913030" blog, please be sure to keep this source http://11923030.blog.51cto.com/11913030/1834251

PHP MVC Framework Core Class

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.