Download the PHP package guilhermecostam/lamiaphp without Composer
On this page you can find all versions of the php package guilhermecostam/lamiaphp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download guilhermecostam/lamiaphp
More information about guilhermecostam/lamiaphp
Files in guilhermecostam/lamiaphp
Package lamiaphp
Short Description A minimalist MVC framework for PHP
License MIT
Informations about the package lamiaphp
LamiaPHP
:izakaya_lantern: A minimalist MVC framework for PHP
Content
- Introduction
- Installation
- Running Lamia
- Models, controllers and views
- Model
- Controller
- View
- Routes
- Introducing routes
- Merging routes
- Database
- PDO
- Migrations
- Validating
- Introducing validations
- Validation list
- Testing
- How to contribute
- License
Introduction
Whenever I would start a simple PHP project without the need for a big framework to help me in the development process I would do the whole process of creating a scope, folder structure, settings and more. So to solve this I had the idea to create a simple base template for these occasions. However, as I was developing, I noticed that the project stopped being a template and became a framework. And so Lamia was born, a minimalist framework written in PHP.
Installation
To install Lamia, you must have the following dependencies on your machine:
- PHP 8.0 (or later)
- Composer
- Docker
- Docker-compose
After you have all the dependencies installed, just follow these commands:
Running Lamia
Installation done, now we will run the application. Initially we will create the .env file and fill in the blank information in it.
Note that the database host is already defined.
This is because the database is being run in docker, so the database host is the docker's IP address, which is usually 127.0.0.1
.
If your machine is not
127.0.0.1
, just runifconfig
(linux) oripconfig
(windows) to find out which IP your docker is running on.
Now we will install the composer dependencies and run the containers.
Finally, if all the steps have been followed correctly, the application is already running on localhost and ready to receive the database migrations.
Models, controllers and views
Model
The models must be in the app/Models/
directory and extend the Model abstract class:
The models in the application serve to communicate the PDO section.
Controller
The controllers must be in the app/Controllers/
directory and extend the Controller abstract class:
The controllers in the application serve to intermediate the requests sent by the Models. This is also where the validations of the data provided on the forms are made.
View
The views must be in the app/Views/
directory and is the layer where the visual information of the application should be contained. Only templates should come here, CSS styles and JavaScript files that can be accessed publicly should go in the public/
directory.
To render the views you need to import the view class where you want to call it and call the render
method with the specified directory:
Note that here you only need to specify the folder created for the view and the file name without the
.php
, because therender
function already accesses theapp/Views/
directory directly, along with the extension.
If you want to send information to the View scope, just create an array containing it. To access it in the view, just call the variable $args
specifying the key sent:
Finally, views also render http errors. Just create the view with the error you want in the app/Views/error/
directory and call the errorCode
function passing the error as an integer. It also has a redirect
method which redirects to the url passed as a parameter:
It is worth noting that the
errorCode
function also takes arguments likerender
.
Routes
Introducing routes
The application routes must be in the
routes/
directory. The correct way to write them is to separate them into files that have nomenclatures related to the grouped routes. For example, the routes responsible for a coffee CRUD should be contained in thecoffee.php
file, and those for a beer CRUD should be contained inbeer.php
.
However, nothing stops you from putting all of the application's routes into a single file.
To create routes is simple. Just include the Router classes and the controllers that have the functions to be used:
After that, simply create an array $routes
and structure it as follows, calling the Router's createRoute
function:
In case of fetching an undefined route, the application is already prepared to return a 404 error view.
Merging routes
For the system to recognize the routes created, it is necessary to merge the files created in the web.php
file:
Database
PDO
For the database MySQL is being used. We will use PDO to communicate with the database and create queries.
In the abstract class Model the connection to the database is already made with the instance of the Connection class. Just call the db
attribute in the class that extends model and call the following methods:
It is worth pointing out that it is also possible to bind values with the mentioned methods. Just create the following query, passing also an array with parameter that are the values to be replaced:
Migrations
In Lamia you can use migrations via Phinx. The migrations automatically go into the database/migrations/
directory by creating them with the following command:
For more details on writing migrations and the rest of Phinx's functionality, see its documentation.
Validating
Introducing validations
If you want to validate the data in a form, Lamia has a native functionality for this. Just create files to write the rules to in the app/Requests/
directory. In each file you must extend the abstract class Request:
To perform validations, you must create a public method validations
. This will return an array_merge
with the return of each validated field.
The validate
method called in the merge must receive two parameters. These are the name of the field to be validated and an array of validations that should be applied to the value assigned to the field.
Observations:
- The array that 'validate' receives must have items where the key is the name of the validation predefined in the application and the value is the message to be displayed if the validation fails.
- Note that each call of the extended
validate
method of Request is for one field of the form.
Now just include the request where you want to perform the validations and call the validations
method:
Validation list
Here is a list of all the validations available in the application:
Name | Description |
---|---|
required | Validate if value is empty |
isArray | Validate if value is an array |
isString | Validate if value is an string |
isInteger | Validate if value is an integer |
isFloat | Validate if value is an float |
isBoolean | Validate if value is an boolean |
isEmail | Validate if value is an boolean |
isUrl | Validate if value is an url |
isValidDate | Validate if value is a valid date |
Testing
For the unit tests PHPUnit is being used. The tests should be written in the tests/
directory and to run them just run the following command:
For more details on writing tests, assertion lists, and the rest of PHPUnit's functionality, see its documentation.
How to contribute
Do you want to contribute to the Lamia? Just follow these instructions:
- Fork this repository.
- Clone your repository.
- Create a branch with your feature:
git checkout -b my-feature
- Commit your changes:
git commit -m 'feat: My new feature'
- Push to your branch:
git push origin my-feature
- Come in Pull Requests from the original project and create a pull request with your changes for
dev
branch.
After the merge of your pull request is done, you can delete your branch and wait for the feedback.
License
This project is licensed under the MIT License - see the LICENSE page for details.