dinhthibc/feazy

一个简单的PHP框架

1.0.3 2018-11-04 04:21 UTC

This package is auto-updated.

Last update: 2024-09-04 17:36:08 UTC


README

一个简单且轻量级的PHP框架,用于构建Web应用和RESTful API

安装

$ composer require dinhthibc/feazy

简单应用

添加简单的index.php文件

<?php
define('ROOT_PATH', __DIR__);
$loader = require ROOT_PATH . '/vendor/autoload.php';

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->get('/', function() {
	echo 'Welcome to Feazy';
});
?>

然后运行服务器

$ php -S localhost:8000

在网页浏览器中打开localhost:8000

具有控制器的简单应用

添加文件夹

$ mkdir src/Application/Controller

UserController类添加到控制器文件夹

<?php

namespace Application\Controller;

use Feazy\Controller\Controller;

class UserController extends Controller
{
	public function index() {
		echo 'This is users page';
	}

	public function get($id) {
		echo "Get user details with id = $id";
	}
}
?>

以及index.php文件

<?php
define('ROOT_PATH', __DIR__);

$loader = require ROOT_PATH . '/vendor/autoload.php';
$loader->addPsr4('', ROOT_PATH . '/src');

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->get('/', function() {
	echo 'Welcome to Feazy';
});

$router->group('/users', function(\Feazy\Common\Router $router) {
	$router->get('/', 'UserController@index');
	$router->get('/(\d+)', 'UserController@get');
});

?>

然后运行服务器

$ php -S localhost:8000

在网页浏览器中打开localhost:8000/userslocalhost:8000/users/1

路由器

本节将帮助您了解如何配置您的路由器

安装新的路由器

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);

您可以使用基本的路由,如下所示

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->basicRoute();

或定义自己的路由,如下所示

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->group('/users', function(\Feazy\Common\Router $router) {
	$router->get('/', 'UserController@index');
	$router->get('/(\d+)', 'UserController@get'); //using regex
});

$router->group('/api', function(\Feazy\Common\Router $router) {
	$router->setPrefix('\\Application\\API\\'); //set namespace for controller
	$router->get('/users', 'UserController@index');
});

视图

渲染布局

本节将帮助您使用1个布局文件来组织视图文件。让我们看看以下文件结构

.
├── index.php
├── src
│   └── Application
│      └── Controller
│         └── IndexController.php
├── template
│   └── layout
|      └── layout.phtml
│   └── index
|      └── index.phtml
└── vendor

使用以下代码为每个文件添加内容:index.php

<?php
define('ROOT_PATH', __DIR__);

$loader = require ROOT_PATH . '/vendor/autoload.php';
$loader->addPsr4('', ROOT_PATH . '/src');

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
\Feazy\Common\DIManager::add('view', new \Feazy\Common\View('template'));

$router->get('/', 'IndexController@index');
?>

IndexController.php

<?php
namespace Application\Controller;
use Feazy\Controller\Controller;
class IndexController extends Controller
{
	public function index() {
		$this->view->setTitle('Homepage');
		$this->view->render('index/index', [
			'myVariable' => 'This is a variable value'
		]);
	}
}

layout.phtml

<html>
<head>
	<!--- css/meta -->
	<title><?php echo $this->title; ?></title>
</head>
<h3>Layout title</h3>
<div class="content">
	<?php require_once ($this->content); ?>
</div>
</html>

index.phtml

myVariable => <?php echo $myVariable; ?>

当您在浏览器中运行网站时,您将得到以下结果

Layout title
myVariable => This is a variable value

渲染视图部分

在多个子页面中管理您的常用部分。现在我们在目录中有一些额外的文件

.
├── index.php
├── src
├── template
│   └── layout
│   └── index
|      └── index.phtml
│   └──  sections
│      └── component1.phtml
│      └── component2.phtml
└── vendor

更新index.phtml代码

<div>
	<?php $this->addSection('sections/component1'); ?>
</div>
<div>
	<?php $this->addSection('sections/component2', ['myVariable']); ?>
</div>

component1.phtml

This is component 1 content

component2.phtml

myVariable => <?php echo $myVariable; ?>

视图变量

帮助我们从一个控制器向视图公开变量

class IndexController extends Controller
{
	public function index() {
		$this->view->setTitle('Homepage');
		$this->view->render('index/index', [
			'myVariable' => 'This is a variable value'
		]);
	}
}

在视图子页面中,只需调用以下块即可获取$myVariable的值

<?php echo $myVariable; ?>

待续