mkdir app01 cd app01
Telepítjük az aktuális könyvtárba a Leaf-t a vendor könyvtárba.
composer require leafs/leaf
A parancs után létrejön egy vendor nevű könyvtár. A leaf ebbe könyvtárba kerül függőségként.
Szükségünk van egy $app objektumra, amivel létrehozhatjuk az útválsztást:
<?php require __DIR__ . '/vendor/autoload.php'; $app = new Leaf\App; $app->get('/msg', function() use($app) { $app->response()->json(['msg' => 'Hi!']); }); $app->run();
Az $app objektum helyett használhatjuk az app() függvényt:
<?php require __DIR__ . '/vendor/autoload.php'; app()->get('/msg', function() { response()->json(['msg' => 'Hi!']); }); app()->run();
php -S localhost:8000 -t .
Tesztelés HTTPie kliens segítségével:
http localhost:8000/msg
Hozzunk létre, például a projekt gyökérkönyvtárában egy EmplyoeeController.php fájlt:
<?php class EmployeeController { public function index() { response()->json('műkszik'); } }
<?php require __DIR__ . '/vendor/autoload.php'; $app = new Leaf\App; require "EmployeeController.php"; $app->get('/employees', 'EmployeeController@index'); $app->run();
<?php require __DIR__ . '/vendor/autoload.php'; require 'api.php'; app()->run();
<?php require "EmployeeController.php"; $app = new Leaf\App; $app->get('/employees', 'EmployeeController@index');
<?php class EmployeeController { public function index() { response()->json('műkszik'); } }
composer require leafs/db
db()->connect([ 'dbtype' => 'sqlite', 'dbname' => 'db.sqlite', ]);
// syntax db()->connect('hostname', 'dbnev', 'user', 'titok', 'mysql');
Lekérdezés:
$emps = db()->query('SELECT * FROM employees')->all();
drop table if exists employees; create table employees ( id int unsigned not null primary key auto_increment, name varchar(50), city varchar(50), salary double, created_at timestamp default current_timestamp, updated_at timestamp default current_timestamp on update current_timestamp );
Célszer a kontrollerek számára készíteni egy app/controllers könyvtárat. A TodoController osztályban a konstruktorban állítsuk be az SQLite-t:
<?php class TodoController { public function __construct() { db()->connect([ 'dbtype' => 'sqlite', 'dbname' => 'db.sqlite' ]); } public function index() { $todos = db()->select('todos')->all(); response()->json($todos); } public function create() { $res = db() ->insert('todos') ->params([request()->body()]) ->execute(); $id = db()->lastInsertId(); $todo = db()->select('todos')->where('id', $id)->first(); response()->json($todo); } public function update($id) { $res = db() ->update('todos') ->params(request()->body()) ->where('id', $id) ->execute(); $todo = db()->select('todos')->where('id', $id)->first(); response()->json($todo); } public function delete($id) { $res = db() ->delete('todos') ->where('id', $id) ->execute(); response()->json($res); } }
<?php require "app/controllers/TodoController.php"; app()->get('/api/todos', 'TodoController@index'); app()->post('/api/todos', 'TodoController@create'); app()->put('/api/todos/{id}', 'TodoController@update'); app()->delete('/api/todos/{id}', 'TodoController@delete');
composer require leafs/auth
<?php class AuthController { public function __construct() { auth()->connect('', 'db.sqlite', '', '', 'sqlite'); } public function login() { $data = auth()->login([ 'username' => request()->get('username'), 'password' => request()->get('password') ]); response()->json(['auth' => $data]); } public function register() { $data = auth()->register([ 'username' => request()->get('username'), 'password' => request()->get('password') ], ['username']); if(!$data) { $data = auth()->errors(); } response()->json(['auth' => $data]); } }
<?php auth()->connect('', 'db.sqlite', '', '', 'sqlite'); app()->registerMiddleware('auth', function () { $id = auth()->id(); if (!$id) { response()->exit([ 'status' => 'error', 'message' => 'Not authorized' ], 401); } });
<?php require "app/controllers/TodoController.php"; require "app/controllers/AuthController.php"; require "app/middlewares/Auth.php"; app()->get('/api/todos', 'TodoController@index'); app()->post('/api/login', 'AuthController@login'); app()->post('/api/register', 'AuthController@register'); app()->group('/', ['middleware' => 'auth', function () { app()->post('/api/todos', 'TodoController@create'); app()->put('/api/todos/{id}', 'TodoController@update'); app()->delete('/api/todos/{id}', 'TodoController@delete'); }]);
composer require leafs/cors
vagy:
leaf install cors
<?php require __DIR__ . '/vendor/autoload.php'; require 'app/routes/api.php'; app()->cors(); app()->run();
Beállítás:
app()->cors([ 'origin' => 'http://example.com', 'optionsSuccessStatus' => 200 ]);