PHPackages                             sevval42/spade - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. sevval42/spade

ActiveLibrary[Framework](/categories/framework)

sevval42/spade
==============

A lightweight PHP Framework for personal projects

v0.1.0(3mo ago)11MITPHPCI passing

Since Jan 25Pushed 3mo agoCompare

[ Source](https://github.com/Sevval42/Spade)[ Packagist](https://packagist.org/packages/sevval42/spade)[ RSS](/packages/sevval42-spade/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

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
------------

[](#installation)

Install will be available via composer

```
composer require sevval42/Spade
```

If you want to try this example repository out, clone it and run docker:

```
docker compose up -d --build
docker compose run --rm app composer install
docker compose run --rm app ./vendor/bin/captainhook install --force

```

The routes in `routes/web.php` can be tried out at

Basic usage
-----------

[](#basic-usage)

This section will describe the base functionality provided by this framework, as well as give some short examples and restrictions.

### Router.php

[](#routerphp)

A route can be added to the Router by specifying the method, route and a callable, for example like this:

```
$router->addRoute('GET', '/users/{id:\d+}', [UserController::class, 'handle']);
```

The route parameter uses `{variableName:regex}` for specifying attributes. The `handle` method of the given Controller can then use the attributes like this:

```
public function handle(int $id): Response
{
    ...
}
```

You can dispatch a request like this:

```
public function handle(Request $request): Response
{
    $method = $request->getMethod();
    $uri = $request->getUri();

    try {
        return $this->router->dispatch($method, $uri);
    } catch (RouteNotFoundException $exception) {
        return new NotFoundResponse();
    } catch (RouteNotAllowedException $exception) {
        return new InternalServerErrorResponse();
    }
}
```

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

[](#responses)

There are a few basic Response classes, this framework provides. Simple message Responses can use the base `Response`class, while the frameworks templating uses the `ViewResponse` which will be further discussed in that section.

### Dependency Injection

[](#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

[](#database-layer)

Spade has made a few abstractions, although it does not have a query builder yet.

#### Connection.php

[](#connectionphp)

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

[](#queries)

```
$connection->query('SELECT * FROM user WITH id=:id', ['id' => 4]);
$result = $connection->fetchOne();
```

##### Updates

[](#updates)

```
$connection->update('user', ['first_name' => 'john', 'last_name' => 'cool'], 4);
```

##### Deletes

[](#deletes)

```
$connection->delete('user', 4);
```

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

[](#baserepositoryphp-and-baseentityphp)

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,

```
create table Users (
	id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
	first_name VARCHAR(50),
	last_name VARCHAR(50),
	email VARCHAR(50),
	birth_date DATE
);
```

maps to

```
