①新建一个文件夹,然后在里面按住 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