Home  >  Article  >  Backend Development  >  A brief analysis of the implementation source code and usage tutorial of Niuniu PHP

A brief analysis of the implementation source code and usage tutorial of Niuniu PHP

PHPz
PHPzOriginal
2023-04-12 09:20:491999browse

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

  1. Directory structure

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. Core file code

(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(&#39;BASEPATH&#39;, dirname(__FILE__) . &#39;/&#39;);
define(&#39;SYSTEMPATH&#39;, BASEPATH . &#39;system/&#39;);
define(&#39;APPPATH&#39;, BASEPATH . &#39;application/&#39;);

require_once(SYSTEMPATH . &#39;core/Niu.php&#39;);
$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');
    }
}
  1. Template engine usage

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.

  1. Controller creation

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!";
    }
}
  1. View creation

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>
  1. Routing configuration

Open the application/config/routes.php file and add the following code:

$route['default_controller'] = 'hello';
$route['404_override'] = '';
  1. Run the test

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn