Идеал достигается не тогда, когда нечего добавить, а тогда, когда нечего убрать
Your Application.php file will look like this approximately:
//---------------- // application.php //---------------- class Application { static function run() { session_start(); $dbFile = __DIR__ . "/db.sqlite"; Database::connect($dbFile); Routes::handle(); } }
And Application class will be used in your index.php:
//---------- // index.php //---------- require_once(__DIR__.'/autoload.php'); Application::run();
Create each model as a class in a separate file - usual approach. What is not usual: this framework advice you to create models as a facade. All functions in class are static. I think this style can be called facade-based style.
//------------ // @movies.php //------------ class Movies { static $table = "movies"; static function add($values) { return database::add(self::$table, $values); } static function remove($rowid) { return database::remove(self::$table, $rowid); } static function update($rowid, $values) { return database::update(self::$table, $rowid, $values); } static function get($rowid) { return database::get(self::$table, $rowid); } static function show($limit = 200) { $table = self::$table; return database::queryAll("select * from $table limit ?", [$limit]); } static function findByActor($actor_id) { $table = self::$table; return database::queryAll("select * from $table where actor_id=?", [$actor_id]); } }
Create each controller as a class in a separate file - usual approach. What is not usual: this framework advice you to create controllers as a facade. All functions in class are static. (facade-based style again).
//---------------------- // @moviescontroller.php //---------------------- class MoviesController { static add() { $rowid = Movies::add($_POST["values"]); Response::json(Movies::get($rowid)); } static remove() { $rowid = $_POST["id"]; Response::json(Movies::remove($rowid)); } static update() { $rowid = $_POST["id"]; Response::json(Movies::update($rowid, $_POST["values"])); } static get() { $rowid = $_POST["id"]; Response::json(Movies::get($rowid)); } static findByActor() { $actorId = $_POST["actorId"]; Response::json(Movies::findByActor($actorId)); } }
In javascript you can form ajax request like this:
//--------------- // main.js //--------------- window.addEventHandler("load", () => { ajax("api-movies-add", {values: {title: "Pink Panther"}}, (json) => { console.log("Movie Added:", json); }); ajax("api-movies-get", {id: 1}, (json) => { console.log("Movie Returned", json); }); ajax("api-movies-update", {id: 1, values: {title: "New Title"}}, () => { console.log("Movie Updated"); }); ajax("api-movies-find-by-actor", {actorId: 2}, (result) => { console.log("Movie Found:", result); }); });
In the .htaccess you should configure URL handling like this:
//--------- //.htaccess //--------- RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?view=$1 [QSA]
And your Routes.php file could looks like this:
//----------- // routes.php //----------- class Routes { static function handle() { self::handleApiRoutes(); self::handleViewRoutes(); } static function handleApiRoutes() { $view = $_GET["view"]; route::handle($view, "api-movies-add", "MoviesController::add"); route::handle($view, "api-movies-get", "MoviesController::get"); route::handle($view, "api-movies-update", "MoviesController::update"); route::handle($view, "api-movies-find-by-actor", "MoviesController::findByActor"); } static function handleViewRoutes() { $view = $_GET["view"] ?: "index"; include("$view.html"); } }
Support Email: