Download the PHP package sevval42/spade without Composer
On this page you can find all versions of the php package sevval42/spade. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sevval42/spade
More information about sevval42/spade
Files in sevval42/spade
Package spade
Short Description A lightweight PHP Framework for personal projects
License MIT
Informations about the package spade
Spade
A lightweight PHP framework for learning purposes and small personal projects.
This framework provides dependency injection, request and response classes and a router, a simple ORM and very simple templating. Examples can be seen in this projects src directory.
Note: Spade is intended for educational use and small projects, it therefore is not optimized or secure in any way.
Installation
Install will be available via composer
If you want to try this example repository out, clone it and run docker:
The routes in routes/web.php can be tried out at http://localhost
Basic usage
This section will describe the base functionality provided by this framework, as well as give some short examples and restrictions.
Router.php
A route can be added to the Router by specifying the method, route and a callable, for example like this:
The route parameter uses {variableName:regex} for specifying attributes. The handle method of the given Controller can then use the attributes like this:
You can dispatch a request like this:
All this is actually handled by the App class, which can be called to initialize and dispatch routes with the router, by calling its initRoutes(array $routes) and handle(Request $request) methods.
Responses
There are a few basic Response classes, this framework provides. Simple message Responses can use the base Responseclass, while the frameworks templating uses the ViewResponse which will be further discussed in that section.
Dependency Injection
The dependency injection is handled by the Container.php class. You can set the necessary base dependencies with the set(YourClass::class, fn() => $yourClass) method, and then get(YourClass::class) objects of the given class.
This should be setup in the entry point of your program, an example can be seen in public/index.php.
Database layer
Spade has made a few abstractions, although it does not have a query builder yet.
Connection.php
This is the base class, that abstracts the PDO class given by PHP. It gives the user multiple methods to do simple queries, like:
Queries
Updates
Deletes
This layer should not be used for entity handling though, as this is handled by the BaseRepository and EntityManager classes:
BaseRepository.php and BaseEntity.php
This frameworks ORM maps database tables to Classes which extend from the abstract BaseEntity class, thus Entity has the $id identifier. Furthermore, each entity class needs to implement the getTableName(): string method, which returns the database table name.
Class Attributes need to be snake_case in the database and camelCase in the Entity. For example,
maps to
The abstract BaseRepository.php class gives functionality for reading data and implements find(int $id) and fetchAll() methods. Classes that extend from this BaseRepository need to implement the getEntity() method, which returns the Entity class-string, this repository watches.
A UserRepository could look like this:
EntityManager.php
To persist changes in the database, the EntityManager is used. It automatically tracks objects which extend from the BaseEntity class and that are fetched from the database using the Hydrator.php class.
New entities can be persisted with the persist($entity) method and removed with the remove($entity).
Any changes will be written to the database, when calling the flush method: $entityManager->flush().
Templating
Spade provides very simple templating with pages and partials. It does not have any logic yet. The TemplateService.php class needs to be initialized with the paths to the pages and partials like this (example):
These templates can than be returned by the ViewResponse class like this:
where the info.php template is in src/Templates/Pages/user/info.php.
A simple page might look like this (info.php):
where variables with three curly braces (like {{{ user.firstName }}}) are not escaped.
The given data array might look like this for the given example:
If a variable is not filled in the $data array, the template leaves the variable empty.
License
This project is licensed under the MIT License. See the LICENSE file for details.