phpgram/mvc

基于 phpgram 构建的 PHP MVC 框架

1.5.3 2020-08-07 10:19 UTC

This package is auto-updated.

Last update: 2024-09-07 23:43:57 UTC


README

phpgram mvc 项目

Packagist Version PHP from Packagist Packagist

一个快速且轻量级的 MVC 框架

基于 phpgram 微型框架构建

特性

  • 简单快速的 Http 路由

  • Psr 7 请求和响应,使用 Nyholm

  • Psr 15 中间件支持

  • Psr 17 工厂,使用 Nyholm

  • 依赖注入使用 Psr 11 容器。容器来自 pimple

  • PHP 模板系统

  • 基本的登录、认证和用户管理通过 PHP 会话(或 Psr 16 缓存)

  • 基本的 Psr 7 Cookies

  • 异步请求准备就绪

安装

通过 composer

composer create-project phpgram/mvc

初始化/设置

本地

  1. 复制文件:env.local.php.dist 并将其重命名为:env.local.php

  2. 完成 数据库路径 信息

    • 对于 ROOT_URL_PATH = 相对 URL 路径(例如:hello.de/my_folder/ -> /my_folder 是路径)
  3. 完成 :)

部署

将 env.php 改为您的部署服务器

文档

定义路由

<?php
//file /routes/web.routes.php

use Gram\Project\App\AppFactory as Route;

use App\Http\Controller\DummyController;

//With function as handler
// for Url: / its return: This page is the Start
Route::get("/",function (){
	return "This page is the Start";
});

//with Controller (Class) as handler
//at Url: /controller: instantiate class DummyController (incl Dependency Injection) and call the method dummyFunction()
Route::get("/controller",DummyController::class."@dummyFunction");

//call this method with the value of id
Route::get("/user/{id}",DummyController::class."@dummyFunctionWithArgs");

定义控制器

<?php
//file: app/Http/Controller/DummyController.php

namespace App\Http\Controller;

use Gram\Mvc\Lib\Controller\BaseController;

class DummyController extends BaseController
{
	private $tpl = 'index.temp';	//template without the .php!

	public function dummyFunction()
	{
		return $this->view($this->tpl,[
			'msg'=>"Test Controller"
		]);
	}
	
	public function dummyFunctionWithArgs($id)
	{
		return $this->view($this->tpl,[
			'userId'=>$id
		]);
	}
}

定义模板

<?php
	//file index.temp.php
$this->extend('defaultview'); //Values will being save for defaultview

$this->assign('h1',"Headline");	//assign variable h1 with the value Headline

//assign multiple variables at once
$this->assignArray([
	'title'=>"Test page",
	'my_awesome_button'=>"<button>Button</button>"
]);
?>

<?php 
//Starts a section. The content will be buffered
$this->start();?>
	<script type="text/javascript">
		alert("Hello! I am an alert box!!");
	</script>

	<style></style>
<?php 
//the content of this section will be available as variable head
$this->end('head');?>


<?php $this->start();?>
	<h2>Test Template</h2>
	
	<p>Welcome to phpgram mvc framework!</p>
<?php $this->end('content');?>

<?php
	//file defaultview.php
?>

<html>

<head>
	<?= $title?>
	<?= $head?>
</head>

<body>
	<h1><?= $h1?></h1>
	
	<div class="content">
		
		<p> <?= $content?> </p>
    	
    	<?= $my_awesome_button?>
    	
	</div>
</body>

</html>

许可协议

phpgram 是开源的,遵循 MIT 许可协议