With the continuous development of Internet technology, PHP has gradually become one of the most popular programming languages in Web development. Niuniu PHP is a lightweight development framework based on PHP. Its emergence has brought great convenience and efficiency improvement to web developers. This article will introduce you to the implementation source code and usage tutorial of Niuniu PHP.
1. Niuniu PHP implementation source code
The directory structure of Niuniu PHP is basically similar to that of ordinary PHP projects, including application, system , public and other folders. The application folder is the directory that developers want to operate, and core files such as Controller, Model, and View are placed in it. The system folder is the core code of Niuniu PHP, and the public folder stores static files, such as js, css, images, etc.
(1) index.php
index.php is the entry file of the entire project. Its main job is to introduce Niu Niu PHP's core code and parses the request. The specific code is as follows:
<?php define('BASEPATH', dirname(__FILE__) . '/'); define('SYSTEMPATH', BASEPATH . 'system/'); define('APPPATH', BASEPATH . 'application/'); require_once(SYSTEMPATH . 'core/Niu.php'); $app = new Niu(); $app->run();
(2) Niu.php
Niu.php is the core class of the entire NiuNiu PHP framework. This class implements request parsing, routing and controller loading, etc. Function. The code of the core class is as follows:
<?php class Niu { public function run() { $route = $this->loadRoute(); $controllerName = $route['controller']; $methodName = $route['method']; $params = $route['params']; $controllerFilePath = APPPATH . 'controllers/' . $controllerName . '.php'; if (!file_exists($controllerFilePath)) { die('Controller not found: ' . $controllerFilePath); } require_once($controllerFilePath); $controller = new $controllerName(); if (!method_exists($controller, $methodName)) { die('Method not found: ' . $methodName); } call_user_func_array(array($controller, $methodName), $params); } private function loadRoute() { $uri = $_SERVER['REQUEST_URI']; $index = strpos($uri, '?'); if ($index !== false) { $uri = substr($uri, 0, $index); } $uri = trim($uri, '/'); if (empty($uri)) { $uri = 'index'; } $segments = explode('/', $uri); $controller = ucfirst(strtolower($segments[0])); $method = isset($segments[1]) ? $segments[1] : 'index'; $params = array_slice($segments, 2); return array( 'controller' => $controller, 'method' => $method, 'params' => $params ); } }
(3) Controller.php
Controller.php is the controller base class in the MVC framework, and all controllers inherit from this class. Some common controller methods are implemented in this class, such as rendering views, passing variables, etc. The specific code is as follows:
<?php class Controller { protected $data = array(); protected function assign($key, $value) { $this->data[$key] = $value; } protected function render($view, $data = null) { if ($data !== null) { $this->data = array_merge($this->data, $data); } extract($this->data); require_once(APPPATH . 'views/' . $view . '.php'); } }
In addition to its own PHP native view template, Niuniu PHP also supports some common template engines, such as Smarty , Blade et al. To use the template engine, just call the corresponding compilation method in the controller.
For Smarty template engine, the code is as follows:
require_once(APPPATH . 'libs/smarty/Smarty.class.php'); $smarty = new Smarty(); $smarty->setTemplateDir(APPPATH . 'views/'); $smarty->setCompileDir(APPPATH . 'cache/'); $smarty->assign('title', '牛牛PHP'); $smarty->assign('content', 'Hello World!'); $smarty->display('index.tpl');
For Blade template engine, the code is as follows:
require_once(APPPATH . 'libs/blade/Blade.php'); $viewPath = APPPATH . 'views/'; $cachePath = APPPATH . 'cache/'; $blade = new \eftec\bladeone\BladeOne($viewPath, $cachePath); $data = array( 'title' => '牛牛PHP', 'content' => 'Hello World!' ); echo $blade->run('index', $data);
2. Niuniu PHP tutorial
For better To understand the implementation principles of Niuniu PHP, the author below will provide you with some tutorials on using Niuniu PHP for development.
Create a file named Hello.php in the application/controllers directory and add the following code:
<?php class Hello extends Controller { public function index() { echo "Hello World!"; } }
Create a file named hello.php in the application/views directory and add the following code:
<!DOCTYPE html> <html> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $content; ?></h1> </body> </html>
Open the application/config/routes.php file and add the following code:
$route['default_controller'] = 'hello'; $route['404_override'] = '';
Execute php -S in the project root directory Start the server with the localhost:8000 command, and access http://localhost:8000/ in the browser to see the output "Hello World!" string.
3. Summary
Niuniu PHP is a lightweight PHP framework. Its implementation source code is very concise and clear, focusing on development efficiency and ease of use. Through in-depth understanding and learning of the source code and use of Niuniu PHP, we can better improve the efficiency and quality of web development, which is a skill worth mastering.
The above is the detailed content of A brief analysis of the implementation source code and usage tutorial of Niuniu PHP. For more information, please follow other related articles on the PHP Chinese website!