上一节(http://blog.csdn.net/mxdzchallpp/article/details/79219992)成功路由解析,解析成功后,需要实现具体到哪个控制器下的哪个方法。
创建控制器
1.在D:\WAMP\wamp\www\framework下创建app文件夹,进入app文件夹,创建index文件夹,进入index文件夹,分别创建controller、model、view文件夹:
cd D:\WAMP\wamp\www\framework
mkdir app
cd app
mkdir index
cd index
mkdir controller
mkdir model
mkdir view
在controller文件夹下创建index.php,内容如下:
<?php
namespace index\controller;
class index {
public function index() {
echo 'hello';
}
}
2.路由解析到指定的控制器方法。
路由解析三层结构,比如index/user/getUser即是访问index下的user控制器下的getUser方法,slim中的路由配置'/{name}'表示接收name变量。修改D:\WAMP\wamp\www\framework\frame\App.php,内容如下:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// 载入composer自动加载
require ROOT_PATH . '/vendor/autoload.php';
class App {
public static function run() {
// 启用 slim路由
$configuration = [
'settings' => [
'displayErrorDetails' => true, // 开启错误信息
],
];
$c = new \Slim\Container($configuration);
$app = new \Slim\App($c);
$app->get('/', function (Request $request, Response $response, array $args) {
echo 'route err.';
});
$app->get('/{platform}/{controller}/{method}', function (Request $request, Response $response, array $args) {
// 新建控制器类 调用控制器方法
$class_name = "{$args['platform']}\\controller\\{$args['controller']}";
$class = new $class_name;
$class->{$args['method']}();
});
$app->run();
}
}
修改D:\WAMP\wamp\www\framework\frame\FrameAutoLoad.php,使其加载控制器类
<?php
class FrameAutoLoad {
/* 自动加载类 */
public static function autoload($class) {
if(false !== strpos($class, '\\')) {
// 加载app下的文件
$path = ROOT_PATH . '/app/' . str_replace('\\', '/', $class) . '.php';
if(file_exists($path)) {
require $path;
return ;
}
return ;
}
// 加载frame框架下的文件
$path = FRAME_PATH . $class . '.php';
if(file_exists($path)) {
require $path;
return ;
}
}
}
浏览器输入http://127.0.0.1/index/index/index,显示hello则表示成功。
3.配置控制器配置文件
在D:\WAMP\wamp\www\framework\app\index\下创建config.php文件和common.php文件,其中config.php文件内容:
<?php
return array(
'app_name' => 'index',
);
common.php文件内容(C方法是获取配置文件的配置项的值):
<?php
function C($key, $get_all = false, $config_key = null) {
if(empty($config_key)) $config_key = '__config';
return $get_all == false ? $GLOBALS[$config_key][$key] : $GLOBALS[$config_key];
}
修改D:\WAMP\wamp\www\framework\frame\App.php中的run方法,使其加载配置文件:
public static function run() {
// 启用 slim路由
$configuration = [
'settings' => [
'displayErrorDetails' => true, // 开启错误信息
],
];
$c = new \Slim\Container($configuration);
$app = new \Slim\App($c);
$app->get('/', function (Request $request, Response $response, array $args) {
echo 'route err.';
});
$app->get('/{platform}/{controller}/{method}', function (Request $request, Response $response, array $args) {
// 加载平台路由下的配置文件
self::loadConfigFile(ROOT_PATH . '/app/' . $args['platform']);
// 新建控制器类 调用控制器方法
$class_name = "{$args['platform']}\\controller\\{$args['controller']}";
$class = new $class_name;
$class->{$args['method']}();
});
$app->run();
}
这时我们在控制器类index.php中的index方法中,echo C('app_name')就会显示"index"。即修改D:\WAMP\wamp\www\framework\app\index\controller\index.php,内容:
<?php
namespace index\controller;
class index {
public function index() {
echo C('app_name');
}
}