用composer 快速打造自己的PHP MVC框架
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用composer 快速打造自己的PHP MVC框架相关的知识,希望对你有一定的参考价值。
①新建一个文件夹,然后在里面按住 shift+右键 打开cmd,输入
> composer init ,然后依次输入信息就会在目录下生成 composer.json如下:
"name": "vendor/composer",
"require":
②输入 composer install 生成下面vendor目录(用于存放composer包),然后再文件根目录下创建 index.php作为入口文件并修改内容如下:
<?php
//引入自动加载文件
require "vendor/autoload.php";
③创建对应的目录,构建基本的MVC模型目录结构如下:
app目录结构如下:
注:app中routes.php用于存放路由
④为composer.json中 require 添加如下包 "noahbuscher/macaw": "dev-master" ,及修改psr加载目录,完成如下:
"name": "vendor/composer",
"require":
"noahbuscher/macaw": "dev-master"
,
"autoload":
"psr-4":
"App\\\\": "app/"
然后 执行composer udpate 和 composer dump-autoload ,执行完后修改 index.php文件如下:
<?php
//引入自动加载文件
require "vendor/autoload.php";
//添加路由
require "app/routes.php";
⑤修改app/routes.php文件如下:
<?php
use \\NoahBuscher\\Macaw\\Macaw;
Macaw::get(/, App\\Controllers\\HomeController@home);
Macaw::dispatch();
并且新建app/Controllers/HomeController.php文件如下:
<?php
namespace App\\Controllers;
class HomeController
public function home()
echo "我是Home文件";
然后执行 php -S localhost:8000 运行该项目,并打开浏览器访问结果如下:
⑥接下来是引入 数据库连接部分,向composer.json的require添加:
"illuminate/database": "*"
然后cmposer update后修改index.php文件如下:
<?php
//引入自动加载文件
require "vendor/autoload.php";
// 引入Eloquent ORM
$capsule = new Illuminate\\Database\\Capsule\\Manager();
$capsule->addConnection(require ./config/database.php);
$capsule->bootEloquent();
//添加路由
require "app/routes.php";
并新建config/database.php文件如下:
<?php
return [
driver => mysql,
host => localhost,
database => test,
username => root,
password => root,
charset => utf8,
collation => utf8_general_ci,
prefix =>
];
⑦然后新建app/Models/Project.php如下:
<?php
namespace App\\Models;
use Illuminate\\Database\\Eloquent\\Model;
class Project extends Model
protected $table = project;
public $timestamps = false;
public function getAll()
return $this->all()->toArray();
并修改HomeController.php中的home 方法如下:
<?php
namespace App\\Controllers;
use App\\Models\\Project;
class HomeController
public function home()
$model = new Project();
var_dump($model->getAll());
注:记得引入对应model,如use App\\Models\\Project;
刷新浏览器结果如下:
⑧引入视图层的模板引擎,向composer.json添加 "xiaoler/blade": "^0.3.1", 然后composer update 更新
并且更新composer.json的psr4如下:
"name": "vendor/composer",
"require":
"noahbuscher/macaw": "dev-master",
"illuminate/database": "*",
"xiaoler/blade": "^0.3.1"
,
"autoload":
"psr-4":
"App\\\\": "app/",
"Tool\\\\": "tool/"
执行composer dump-autoload,然后新建tool/View.php如下:
<?php
namespace Tool;
use Xiaoler\\Blade\\Compilers\\BladeCompiler;
use Xiaoler\\Blade\\Engines\\CompilerEngine;
use Xiaoler\\Blade\\Factory;
use Xiaoler\\Blade\\FileViewFinder;
class View
/**
* 获取视图
* @return Factory
*/
public static function getView()
$compiler = new BladeCompiler($_SERVER[DOCUMENT_ROOT] . /cache);
$engine = new CompilerEngine($compiler);
$finder = new FileViewFinder([$_SERVER[DOCUMENT_ROOT] . /app/Views]);
$factory = new Factory($engine, $finder);
return $factory;
⑨新建视图app/Views/home.blade.php如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> $b </title>
</head>
<body>
<div> $a </div>
</body>
</html>
然后修改HomeController.php如下:
<?php
namespace App\\Controllers;
use App\\Models\\Project;
use Tool\\View;
class HomeController
public function home()
$model = new Project();
//var_dump($model->getAll());
echo View::getView()->make(home, [a => 这是aaa, b => b是标题])->render();
注:引入use Tool/View
然后刷新浏览器效果如下:
至此以上便是全部MVC的教程,详情请查看我的github(里面有最新的更新内容):https://github.com/Lidisam/simple-mvc-for-composer
如何简单快速的使用composer
参考技术A 1设置php.ini,方便安装在从官网下载 Composer-Setup.exe安装程序后,在安装过程中会碰到几个麻烦的地方:
1. 找不到扩展dll文件,修改php.ini文件内extension_dir 参数
2. browscap未找到,修改php.ini文件内 browscap参数
3. SSL dll未找到,修改php.ini文件内";extension=php_openssl.dll",去掉前面“;”
正确的安装画面,如下:
2使用Composer中文镜像网站,替代国外网站
当你安装完Composer后,使用时,缺发现不能下载包,这是一个众所周知的原因。
所以,我们要什么国内的镜像站点进行代替,常用方法有两种:
1. 修改全局配置:全局配置的文件一般放在C:\Users\XXX\AppData\Roaming\Composer\config.json;
"config": , "repositories": [ "type":"composer", "url":"http://pkg.phpcomposer.com/repo/packagist/", "packagist":false ]
2. 修改当前配置:即项目当前的composer.json文件;
以 laravel 项目的 composer.json 配置文件为例:
"name": "laravel/laravel", "description": "TheLaravel Framework.", "keywords": ["framework", "laravel"], "license":"MIT", "type": "project", "require": "laravel/framework":"4.2.*" , "autoload": "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ] , "scripts": "post-install-cmd":[ "php artisanclear-compiled", "php artisan optimize" ], "post-update-cmd":[ "php artisanclear-compiled", "php artisan optimize" ], "post-create-project-cmd":[ "php artisankey:generate" ] , "config": "preferred-install":"dist" , "minimum-stability":"stable", "repositories": [ "type":"composer", "url":"http://pkg.phpcomposer.com/repo/packagist/", "packagist": false ]
以上是关于用composer 快速打造自己的PHP MVC框架的主要内容,如果未能解决你的问题,请参考以下文章
PHP趣味程序 - 利用 Composer 一步一步构建自己的 PHP 框架之设计 MVC