My Simple PHP framework--labphp

Source: Internet
Author: User

The simple PHP framework that I mentioned last time is based on the MVC design pattern-This is the initial list of the labphp simple framework I implemented, which does not use any of the template engines, so if you want to use PHP variables in the template, you still need to use PHP tags and syntax, output display variables. But it basically realizes the separation of logic and interface.

The simplicity of the framework and therefore the existence of many limitations, such as the directory structure can not be easily modified, of course, unless the framework to modify the content of the relationship between the time I did not make too many changes.

The directory structure of the labphp framework is as follows:


The directory structure of the project is as follows:


The entry file is index.php

The URL pattern is:

Http://serverName/index.php?m= Module name/c= controller name/a= operation name

The default module is home, the default controller is index, and the default action is index. If you need to access non-default, you need to pass in parameters.

In the application folder can have more than one module, here is only a list of two modules Home module and Admin module, the default module is the Home module, also because it is the default module, so in the case of access to non-default module, you need to pass in the URL parameters, such as index.php?m= Admin, indicates access to admin module content.

Of course we can add more modules, we need to note that the folder under the module needs to be consistent. Where the config.php file content for the configuration file, Controller folder to place the custom controllers, the model folder to place the custom models, the View folder placement template, where the view folder first create a folder named controller name, and then place the template.

To access the default module home, the Portal file index.php, the results are as follows:


To access the Admin module, the results are as follows:


The function library of the labphp framework is as follows:

labphp/common/functions.php

<?php//prevent illegal characters from entering function daddslashes ($STR) {return (!GET_MAGIC_QUOTES_GPC ())? Addslashes ($STR): $STR;} /** * Get the module name currently being accessed * @return [Type] Current module name */function getmodulename () {return isset ($_get[' m '])? Daddslashes ($_get[' m ']): ' Home '; Initialize module (default home module)}/** * Gets the controller name currently being accessed * @return [type] Current controller name */function Getcontrollername () {return isset ($_get[' C ')? Da Ddslashes ($_get[' C '): ' Index '; Initialize Controller}/** * Obtains the currently accessed operation name * @return [Type] current operation name */function Getactionname () {return isset ($_get[' a '])? Daddslashes ($_get [' A ']) : ' Index '; Initialization Operation}function GetConfig () {$c 1 = include ('./labphp/conf/convention.php '); $c 2 = include ('./application/'). Module_name. '/config/config.php '); $config = Array_merge ($c 1, $c 2); return $config;}  /** * $method method used to invoke the $NAME controller * @param [type] $name controller name * @param [Type] $method method name */function C ($name, $method) {$m = Getmodulename (); require_once ('./application/'. $m. '/controller/'. $name. ' Controller.class.php '); $c = $name. "Controller"; $ct = new $c (); $ct, $mEthod ();} /** * Returns the corresponding model object * @param [type] $name the name of the model class */function M ($name) {$m = Getmodulename (); require_once ('./application/') . $m. '/model/'. $name. ' Model.class.php '); $model = $name. ' Model '; $obj = new $model (); return $obj;}? >
labphp/conf/convention.php

<?php return Array (/* Database settings */    ' db_host ' + ')               ,/  /server address    ' db_name '               = "  ,          // Database name ' Db_user ' + ',//      username    ' db_pwd ' = ', '/          /password    ' Db_port '               = = '  ,        //Port/    * Template settings */    ' tmpl_template_suffix '  = '  . html ',     // Default template file suffix);? >
lapphp/conf/include.list.php

<?php//defines the file that needs to be introduced in advance $paths = Array (' common/functions.php ',                  //function library ' libs/labphp.class.php ',//                 start engine ' libs/ Controller/controller.class.php ',  //Basic controller class ' libs/model/sqlhelper.class.php ',        //Database operation class ' libs/model/ Model.class.php ',            //Basic model Class);? >
labphp/libs/controller/controller.class.php

<?php//Basic Controller class Controller.class.phpclass controllers {private static $tplPath = './application/'. Module_name. '/view/'; The path of the template placement private static $config; User Configuration + default configuration private static $tmpl _suffix; The suffix name of the template is private $data = Array (); Save the variable registered to the template/** * Get configuration information (User Configuration + default configuration), template suffix name */function __construct () {self:: $config = GetConfig (), Self:: $tmpl _suffix = Self:: $config [' Tmpl_template_suffix '];} /** * Verify that the variable exists, is empty * @param [type] $v [variable to be validated] * @return [Type] [true | false] */function Validate ($v) {if (!isset ( $V) | | Empty ($v)) return False;return true; /** * REDIRECT * @param [type] $url [redirected URL address, format Controller name/method name] * @param string $arg [parameters to be passed by redirection] * @return [type] [DESCRI Ption] */function Redirect ($url, $arg = ') {$args = array (); $args [' m '] = module_name! = ' Home '? ' M= '. Module_name: "; Current Module $arr = Split ('/', $url); $args [' c '] = $arr [0]! = ' Index '? ' C= '. $arr [0]: "; Controller name $args[' a '] = $arr [1]! = ' index '? ' A= '. $arr [1]: "; Method name $args[' arg '] = $arg; $to = ' Location:index.php '; $iSfirst = True;foreach ($args as $key = + $value) {if (!empty ($value)) {if ($isFirst) {$to. = '? '. $value; $isFirst = False ;} else {$to. = ' & '. $value;}}} Header ($to); exit;} /** * Register variable on template * @param [type] $key [variable name applied on template] * @param [type] $value [variable corresponding to value] * @return [type] [Reference of current object, supply chain */function Assign ($key, $value) {$this->data[$key] = $value; return $this;} /** * Display page template * @param [type] $page [Template name: Controller name/Method name (under the View folder under the controller name as a folder under the method name of the template)] * @return [type] [Descrip tion] */function display ($page = Controller_name. '/' . Action_name) {if ($this->validate ($this->data)) {Extract ($this->data);} Include_once (self:: $tplPath. $page. Self:: $tmpl _suffix);//Destroy registered variable if ($this->validate ($this->data)) {foreach ($this->data as $key = $value) {unset ($ $key);} $this->data = Array ();} Exit;}}? >
labphp/libs/model/model.class.php

<?php// Model.class.phpclass model {private $sqlHelper; function __construct () {$this->sqlhelper = new SqlHelper ();} /** * Verify that the variable exists, is empty * @param [type] $v [variable to be validated] * @return [Type] [true | false] */function Validate ($v) {if (!isset ( $V) | | Empty ($v)) return False;return true; /** * Execute DQL statement (SELECT statement) * @param [type] $sql SQL statement * @return [type] result set */function execute_dql_res ($sql) {return $th Is->sqlhelper->execute_dql_res ($sql);} /** * Execute DQL statement (SELECT statement) * @param [type] $sql SQL statement * @return [type] returned as an associative array */function Execute_dql_arr ($sql) {RET Urn $this->sqlhelper->execute_dql_arr ($sql);} /** * Execute DML statement (INSERT, UPDATE, DELETE statement) * @param [type] $sql SQL statement * @return [type] 0 for failure; 1 for success; 2 for no row affected */function E XECUTE_DML ($sql) {return $this->sqlhelper->execute_dml ($sql);}? 
labphp/libs/model/sqlhelper.class.php

<?php//Database operation class SqlHelper.class.phpclass SqlHelper {private $mysqli;p rivate $host;//database hostname private $user;// Database user name private $pwd; Database Password private $db; Database name private $port; Port number Public Function __construct () {//Completion of initialization task if (defined (' sae_mysql_db ')) {//Determine if cloud platform $this->host = Sae_mysql_host_ M; $this->user = Sae_mysql_user; $this->pwd = Sae_mysql_pass; $this->db = sae_mysql_db; $this->port = Sae_ Mysql_port;} else {$config = GetConfig (); $this->host = $config [' Db_host ']; $this->user = $config [' Db_user ']; $this->pwd = $ config[' db_pwd '; $this->port = $config [' Db_port ']; $this->db = $config [' db_name '];} $this->mysqli = new Mysqli ($this->host, $this->user, $this->pwd, $this->db, $this->port); if ($this- >mysqli->connect_error) {die (' connection failed '. $this->mysqli->connect_error);} Setting the character set to access the database//The function of this sentence is to ensure that PHP is UTF8 to operate our MySQL database $this->mysqli->query ("SET NAMES UTF8") or Die ($this- Mysqli->error);} /** * Execute DQL statement (SELECT statement) * @param [type] $sql SQL statement * @return [Type] result set */public function Execute_dql_res ($sql) {$res = $this->mysqli->query ($sql) or Die (' Operation Dql lost Defeat '. $this->mysqli->error); return $res;} /** * Execute DQL statement (SELECT statement) * @param [type] $sql SQL statement * @return [type] returns the */public function Execute_dql_arr as an associative array ($sq L) {$arr = Array (), $res = $this->mysqli->query ($sql) or Die (' Operation Dql failed '. $this->mysqli->error); while ($row = $r ES-&GT;FETCH_ASSOC ()) {$arr [] = $row;} $res->free_result (); return $arr;} /** * Execute DML statement (INSERT, UPDATE, DELETE statement) * @param [type] $sql SQL statement * @return [type] 0 for failure; 1 for success; 2 for no row affected */public fun Ction Execute_dml ($sql) {$res = $this->mysqli->query ($sql)/* or Die (' Operation DML failed '. $this->mysqli->error) */;if (! $res) {return 0;//= failed} else {if ($this->mysqli->affected_rows > 0) {return 1;//= success} else {return 2;//indicates that no rows are affected}}}pu Blic function __destruct () {$this->mysqli->close ();}}? >
labphp/libs/labphp.class.php

<?php class Labphp {private static $m;//module private static $c;//controller private static $a;//Operation/** * Get parameters for HTTP request-"module name, controller name, Action Method Name * @return [Type] [description] */private static function init () {self:: $m = isset ($_get[' m '])? Daddslashes ($_get[' m ']): ' Home '; Initialize module self:: $c = isset ($_get[' C '))? Daddslashes ($_get[' C '): ' Index '; Initialize controller self:: $a = isset ($_get[' a ')? Daddslashes ($_get[' A ']): ' Index '; Initialize Operation}/** * According to the controller and method name, create the corresponding controller, call the corresponding method * @return [Type] [description] */public static function start () {self::init (); C (self:: $c, Self:: $a); C is in functions.php and is used to invoke the $ A method of controller $c}}?>
labphp/labphp.php

<?php//define the mode of application-whether it is debug mode if (!defined (' App_debug ') | | App_debug = = False) {error_reporting (0);} Start engine $currentdir = DirName (__file__);//First include file list include_once $currentdir. '/conf/include.list.php ';//include necessary file foreach ($paths as $value) {include_once $currentdir. '/' . $value;} Define the currently accessed module define (' Module_name ', Getmodulename ());//define the currently accessed controller define (' Controller_name ', Getcontrollername ()); /define the currently accessed operation define (' Action_name ', Getactionname ()); Labphp::start ();? >


application/home/config.phpapplication/admin/config.php

<?php return Array (//' configuration item ' = = ' config value '/* ' db_host ' = ' + ',//Set host ' Db_user ' + ',//Set user name ' db_pwd ' = ', '//Set password ' D B_port ' = + ',//Set port number ' db_name ' + ',//Set database name *//* template settings */    * ' tmpl_template_suffix ' + '  ,     / /default template file suffix */); >
application/home/controller/indexcontroller.class.phpapplication/admin/controller/indexcontroller.class.php

<?php class Indexcontroller extends Controller {function index () {echo "<p style= ' width:50%; height:300px; line-height:300px; padding:10px 20px; Font-family:\ "Microsoft Jas black \", \ "Microsoft Yahei\"; font-size:30px; margin:50px Auto; box-shadow:0 0 3px #ABCDEF; Text-align:center; position:relative; ' >hello labphp!<span style= ' line-height:30px; font-size:20px; Position:absolute; bottom:20px; right:20px; ' > Welcome to use lapphp V1.0.0 by dreamboy<span></p> ";}?" >

index.php

<?php//Open debug mode suggest the development phase to open the deployment phase Comment or set to Falsedefine (' App_debug ', false); Require_once './labphp/labphp.php ';? >










My Simple PHP framework--labphp

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.