Composer 搭建MVC框架、Macaw、Eloquent、Blade
一、主要目录
在开始前,我们先来看看自定义框架的目录结构和重要文件位置
app
Controllers
Models
Views
config
route.php
public
index.php
vendor
composer.json
composer.lock
git 地址:https://gitee.com/hxslcc/myMvc
二、安装路由
composer require noahbuscher/macaw
报错
Could not find a version of package noahbuscher/macaw matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.
因为没有发行其它版本,我们这里指定下使用 dev-master开发版
composer require noahbuscher/macaw:dev-master
安装好后, 编辑index.php
<?php
require '../vendor/autoload.php';
use NoahBuscher\Macaw\Macaw;
Macaw::get('/', function() {
echo 'hello word';
});
// 路由设置后,进行调度
Macaw::dispatch();
nginx 添加虚拟主机,指到public目录下index.php入口文件。
如果没设置虚拟主机,直接访问,会输出404
配置好虚拟机,直接访问就会输出hello world
路由的配置,会随着项目的开发,路由配置代码会越来越多。
所以我们把路由的信息,写入到route.php
index.php
直接引入route.php
即可
<?php
// 自动加载
require '../vendor/autoload.php';
// 路由配置
require '../route.php';
三、增加控制器
Controllers
文件夹下新增
BaseController.php
<?php
namespace Controllers;
class BaseController
{
public function __construct()
{
}
}
HomeController.php
<?php
namespace Controllers;
class HomeController extends BaseController
{
public function index()
{
echo "this is index page";
}
}
编辑 route.php
<?php
use NoahBuscher\Macaw\Macaw;
// 需要带上命名空间地址Controllers
Macaw::get('/', 'Controllers\HomeController@index');
// 路由设置后,进行调度
Macaw::dispatch();
至此,控制器配置完毕,直接访问 http://www.mymvc.com/
,正常输出即可。
四、增加模型
这里我们使用的是laravel的illuminate/database
开始安装
composer require illuminate/database
注意:我这里安装后,要求是php7.1.3以上的版本
添加数据库配置文件
/config/database.php
, 记得自己替换数据库参数
<?php
return [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
];
编辑index.php ,新增代码
// 启用ORM
$capsule = new Capsule;
$capsule->addConnection(require '../config/database.php');
$capsule->bootEloquent();
// 路由必须在最底部
require '../config/route.php';
新增一个模型
\app\Models\Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
}
控制器调用模型
\app\Controllers\PostController.php
<?php
namespace App\Controllers;
use App\Models\Post;
class PostController extends BaseController
{
public function index()
{
$post = Post::select('id', 'title')->first();
var_dump($post->toArray());
}
}
模型的使用到这里就结束,记得自己添加控制器对应的路由,访问能正常输出即可。
五、增加视图
有在网上看到大家使用的一个简单的模版引擎,可以参考下学习
https://github.com/sebastianbergmann/php-text-template
但是这里,视图我们用的laravel提取出来的blade模版引擎,有网友已经提取出来了
https://github.com/XiaoLer/blade/wiki
安装模版引擎
composer require xiaoler/blade
注意查看文档,有的与laravel框架相关的关键字和功能被移除了。
添加视图的基类文件
\app\Views\View.php
<?php
namespace App\Views;
use Xiaoler\Blade\Compilers\BladeCompiler;
use Xiaoler\Blade\Engines\CompilerEngine;
use Xiaoler\Blade\Engines\EngineResolver;
use Xiaoler\Blade\Factory;
use Xiaoler\Blade\Filesystem;
use Xiaoler\Blade\FileViewFinder;
class View
{
protected static $blade_path;
protected static $cache_path;
public static function config($blade_path = '\app\Views', $cache_path = '\storage\framework\cache'){
$root = realpath(__DIR__.'/../../');
self::$blade_path = $root.$blade_path;
self::$cache_path = $root.$cache_path;
$file = new Filesystem;
$compiler = new BladeCompiler($file, self::$cache_path);
$resolver = new EngineResolver;
$resolver->register('blade', function () use ($compiler) {
return new CompilerEngine($compiler);
});
$factory = new Factory($resolver, new FileViewFinder($file, [self::$blade_path]));
return $factory;
}
}
添加公共方法,方便调用
\app\Common\function.php
<?php
function view($view, $data){
return \App\Views\View::config()->make($view, $data)->render();
}
添加模版文件
\app\Views\index.blade.php
<html>
<head>
<title>模版测试</title>
</head>
<body>
<h1>标题:{{$post->title}}</h1>
</body>
</html>
控制器中调用模版引擎替换变量
\app\Controllers\PostController.php
<?php
namespace App\Controllers;
use App\Models\Post;
class PostController extends BaseController
{
public function index()
{
$post = Post::select('id', 'title')->first();
echo view('index', compact('post')); # 直接调用view方法
}
}
访问post控制器的index方法,查看浏览器的源码
<html>
<head>
<title>模版测试</title>
</head>
<body>
<h1>标题:Laravel 入门教程</h1>
</body>
</html>
到这里,我们就使用composer搭建了一个简单MVC框架了。