1. Go to this page and download the library: Download phpframe-project/template library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Controllers\Default;
use PHPFrame\BaseController;
class UserController extends Controller
{
public function listAction()
{
$page = $this->getParam('page', 1);
$users = Db::table('users')->paginate(15, ['*'], 'page', $page);
return $this->json(['code' => 0, 'data' => $users]);
}
public function createAction()
{
$data = $this->request->post();
// 验证并创建...
return $this->json(['code' => 0, 'message' => 'created']);
}
}
namespace App\Controllers\Shell;
use PHPFrame\BaseShell;
class DefaultShell extends BaseShell
{
public function testAction()
{
$params = $this->getParams();
$this->output("参数: " . json_encode($params));
$this->log("执行测试命令");
}
}
use PHPFrame\Facades\Route;
use App\Middleware\AuthMiddleware;
// 全局中间件
Route::middleware(new CorsMiddleware());
// 路由级中间件
Route::registerMiddleware('auth', new AuthMiddleware());
Route::handlerMiddleware('App\Controllers\UserController@profile', ['auth']);
use PHPFrame\Facades\Log;
use PHPFrame\Facades\Db;
use PHPFrame\Facades\Cache;
use PHPFrame\Facades\Config;
Log::info('User logged in', ['user_id' => 123]);
$users = Db::table('users')->get();
Cache::set('key', 'value', 3600);
$name = Config::get('app.name');
// 单例服务
app()->set('my_service', function ($c) {
return new MyService($c->get('db'));
});
// 原型服务(每次解析返回新实例)
app()->prototype('request', function ($c) {
return Request::createFromGlobals();
});
// 使用
$service = app('my_service');