Download the PHP package ricardoper/slim4-skeleton without Composer
On this page you can find all versions of the php package ricardoper/slim4-skeleton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ricardoper/slim4-skeleton
More information about ricardoper/slim4-skeleton
Files in ricardoper/slim4-skeleton
Package slim4-skeleton
Short Description A better Slim Framework 4 Skeleton with a sane folder structure where everything is customizable
License MIT
Homepage https://github.com/ricardoper
Informations about the package slim4-skeleton
PHP Slim Framework v4 Skeleton
A super organized (with a sane folder structure) and very customizable skeleton for Slim Framework v4. Use it to start working quickly on a new project.
NOTE: If you want this skeleton with Twig, go to: (https://github.com/ricardoper/slim4-twig-skeleton).
This project use PHP Composer for a fast installation without any trouble.
- PHP >= 7.2
- Customizable with an easy configuration:
- Logger
- Routes
- Handlers
- Middlewares
- Configurations
- Service Providers
- Response Emitters
- Error Handler
- Shutdown Handler
- Sessions
- Controllers
- Global Helpers
- PSR-3 for logging
- Environment variables with PHP dotenv
- Pimple as Dependency Injection Container
- ddumper for development (based on Symfony VarDumper)
- Better Error Handlers (JSON, XML, PlainText - Accept / Content Type)
- Medoo Database Framework (MySQL, PostgreSQL, SQLite, MS SQL Server, ...)
NOTES:
- Sessions Middleware is not enabled by default.
- Medoo is optional and is not enabled by default. You can use another library as a Service Provider. If you want to use this library, don't forget to install the required PDO extensions.
Table of Contents
- How to Install
- Most Relevant Folders
- Global Helpers
- Development Only
- Configurations
- Dot Notation
- Routes
- Controllers
- Helpers
- Middlewares
- Response Emitters
- Models
- Models Helpers
- Services Providers
- Handlers
- Database Support
- Exceptions
- Logging
- Debugging
- Demo
- Benchmarks
How to Install
Run this command from the directory in which you want to install your new Slim Framework v4 Skeleton.
NOTE:
- Replace
[my-app-name]
with the desired directory name for your new application. - Ensure
storage/
is web writeable. - Point your virtual host document root to your new application's
public/
directory.
Most Relevant Folders
- /app : Application code (App Namespace - PSR-4)
- ./Controllers : Accepts input and converts it to commands for the model. Add your Controllers here.
- ./Emitters : Emits a response, including status line, headers, and the message body, according to the environment. Add your Response Emitters here.
- ./Handlers : Handles specified behaviors of the application. Add your Handlers here.
- ./Middlewares : Provide a convenient mechanism for filtering HTTP requests entering your application. Add your Middlewares here.
- ./Models : Manages the data, logic and rules of the application. Add your Models here.
- ./Routes : Maps an HTTP request to a request handler (controller). Add your Routes here.
- ./Services : Define bindings and inject dependencies. Add your Service Providers here.
- /configs : Add/modify your Configurations here
- /public : Add your Assets files here
Global Helpers
env(string $variable, string $default)
- Returns environment variables (using DotEnv)app()
- Returns App instancecontainer(string $name)
- Returns bindings and inject dependencies from Containerconfigs(string $variable, string $default)
- Returns Configs database_path(string $path)
- Returns base path locationapp_path(string $path)
- Returns app path locationconfigs_path(string $path)
- Returns configs path locationpublic_path(string $path)
- Returns public path locationstorage_path(string $path)
- Returns storage path location
Global Helpers for Development Only
d($var1, $var2, ...)
- Dump vars in collapsed mode by defaultddd($var1, $var2, ...)
- Dump & die vars in collapsed mode by defaultde($var1, $var2, ...)
- Dump vars in expanded mode by defaultdde($var1, $var2, ...)
- Dump & die vars in expanded mode by default
Configurations
You can add as many configurations files as you want (/configs
). These files will be automatically preload and merged in the container based on selected environment.
If you have an environment called "sandbox" and you want to overwrite some configuration only for this environment, you need to create a subfolder "sandbox" in /configs
. Something like that /configs/sandbox
. Then create the file that includes the configuration that you need to replace and the respective keys and values inside it.
/configs/logger.php
/configs/local/logger.php
Results of name
for the environment:
- prod : 'app'
- sandbox : 'app'
- local : 'app-local'
- testing : 'app'
NOTE: You can see the example in this framework for the local environment.
Configurations Dot Notation
You can use dot notation to get values from configurations.
/configs/example.php
If you want the host
value for MySQL type:
Routes
Maps an HTTP request to a request handler (Closures or Controllers).
You can add as many routes files as you want (/app/Routes
), but you need to enable these files in /apps/Routes/app.php
file.
You can organize this routes as you like. There is a little Demo that you can see how to organize this files.
Controllers
Accepts input and converts it to commands for the Models.
You can add as many Controllers as you want in a cleaning way (/app/Controllers
).
After add your Controller, you can enable or disable it in your Routes.
NOTE: To have helpers you must extend the Controllers with ControllerAbstract located in \App\Kernel\Abstracts
.
Controllers Helpers
getApp()
- Returns App objectgetContainer(string $name)
- Returns the App ContainergetConfigs(string $name)
- Returns App ConfigsgetService(string $service)
- Returns Service Provider from container by namegetRequest()
- Returns HTTP RequestgetResponse()
- Returns HTTP ResponsesetEmitter(string $name, string $emitter)
: Set a new Response Emitterwrite(string $data)
- Send to outputjson(array $data, bool $sendHeaders)
- Send JSON encoded to output
Middlewares
Provide a convenient mechanism for filtering HTTP requests entering your application.
You can add as many Middlewares as you want in a cleaning way (/app/Middlewares
).
After add your Middleware, you can enable or disable it in configs/middlewares.php
configuration file.
NOTE: Middlewares must respect the MiddlewareInterface located in \Psr\Http\Message
.
Enable it in configs/middlewares.php
:
Response Emitters
Emits a response, including status line, headers, and the message body, according to the environment.
You can add as many Response Emitters as you want in a cleaning way (/app/Emitters
).
After add your Response Emitter, you can enable or disable it globally in configs/emitters.php
configuration or you can add it in the Controller for a specific action.
NOTE: Response Emitters must respect the ResponseEmitterInterface located in \App\Kernel\Interfaces
.
Enable it in configs/emitters.php
:
Models
Manages the data, logic and rules of the application.
You can add as many Models as you want in a cleaning way (/app/Models
).
After add your Models, you use it for, for example, in a Controller.
NOTE: To have helpers you must extend the Model with ModelAbstract located in \App\Kernel\Abstracts
.
Models Helpers
getApp()
- Returns App objectgetContainer(string $name)
- Returns the App ContainergetConfigs(string $name)
- Returns App ConfigsgetService(string $service)
- Returns Service Provider from container by namegetRequest()
- Returns HTTP RequestgetResponse()
- Returns HTTP ResponsegetDb()
- Returns Database object
Services Providers
Define bindings and inject dependencies.
You can add as many Services Providers as you want in a cleaning way (/app/Services
).
After add your Services Provider, you can enable or disable it in configs/services.php
configuration file.
NOTE: Service Providers must respect the ServiceProviderInterface located in \App\Kernel\Interfaces
.
Enable it in configs/services.php
:
Handlers
Handles specified behaviors of the application.
You can override the following Handlers in a cleaning way (/app/Handlers
):
- ErrorHandler (default located in
/app/Handlers/ErrorHandler
) - ShutdownHandler (default located in
/app/Handlers/ShutdownHandler
)
After add your Handler, you can enable or disable it in configs/app.php
configuration file.
Database Support
Medoo is implemented out of box as a Service Provider. The use is optional and is not enabled by default.
To enable database support with Medoo you need to add this library/vendor with Composer:
Once installed you need to enable the Service Provider in configs/services.php
:
Now you are ready to use it...
If you need more details, documentation, api reference, please visit Medoo webpage: https://medoo.in/
NOTES:
- Don't forget to load PDO extensions for your database. For example, if you need MySQL, you need to install
pdo_mysql
PHP extensions. - You can use another library as a Service Provider (Native drivers for MySQLi, PostgreSQL, MongoDB, Redis, ...).
Exceptions
You have some Exceptions out the box, located in \App\Kernel\Exceptions
, than you can use it:
Logging
Logging is enabled by default and you can see all the output in /storage/logs/app-{date}.log
.
You can set this parameters in /.env
or in /configs/app.php
.
Debugging
Debugging is disabled by default. You can set this parameters in /.env
or in /configs/app.php
.
Demo
This skeleton has a little Demo that you can see all this points in action.
Demo URL's:
Benchmarks
Nothing is free, so let's compare the performance loss with Slim Skeleton.
Machine:
Intel® Core™ i5-8400 CPU @ 2.80GHz × 6
16Gb RAM
SSD
Versions:
Ubuntu 20.04 LTS
Docker v19.03.8
nginx 1.17.10
PHP v7.4.3
Zend OPcache enabled
SIEGE 4.0.4
Bench Details:
25 concurrent connections
500 requests per thread
No delays between requests
Command: siege -c25 -b -r500 "URL"
This Skeleton | Slim Skeleton | |
---|---|---|
Transactions | 12500 hits | 12500 hits |
Availability | 100.00 % | 100.00 % |
Elapsed time | 9.16 secs | 8.80 secs |
Data transferred | 0.45 MB | 0.45 MB |
Response time | 0.02 secs | 0.02 secs |
Transaction rate | 1364.63 trans/sec | 1420.45 trans/sec |
Throughput | 0.05 MB/sec | 0.05 MB/sec |
Concurrency | 24.49 | 24.51 |
Successful transactions | 12500 | 12500 |
Failed transactions | 0 | 0 |
Longest transaction | 0.05 | 0.05 |
Shortest transaction | 0.00 | 0.00 |
Enjoy the simplicity :oP
All versions of slim4-skeleton with dependencies
ext-json Version *
psr/log Version ^1.1
slim/slim Version ^4.5
slim/psr7 Version ^1.1
pimple/pimple Version ^3.3
vlucas/phpdotenv Version ^4.1