Enginr is a very lightweight micro-framework for writing Web applications or API quickly.
Strongly inspired by Node.js (and more particularly by its micro-framework Express.js), learning how to use it is therefore very simplified if you know these technologies.
The biggest difference is the use of $
for your variables... Yey...
🚩 Note : This guide is for the alpha version. Many changes can be made in the near future.
This section will show you how to create your first Enginr application.
It is strongly recommended to use composer
to work with Enginr.
composer require enginr/enginr:v1.0.*-alpha
git clone https://github.com/Enginr/enginr.git \
cd enginr \
composer update
To easily create an Enginr project, you can use the manager.
To use it, start by installing it globally on your machine via composer.
composer global require enginr/manager:dev-master
Once the manager is installed. We will create our first project...
For this guide, I will call it myProject
. But you can give it any name you want !
enginr create myProject
You will have to follow a short interactive guide, then once done, your project will be generated !
Once the generation is complete, go (still in your terminal) to the folder of your project (its name corresponds to the name you gave to your project), then launch the server.
cd myProject \
php app.php
If your server has started correctly, go to your browser, then enter the address of your server.
Note : The alpha version of the manager will have given you the address localhost:8000
.
You should see the Enginr welcome page !
The most existing moment... 😁
Let's create our first application !
mkdir myApp \
cd myApp
See installation guide.
You could named your application file like you want. By convention, I will use app.php
for this guide...
touch app.php
This will be the main file of your application.
By analogy, I would say that it is in a way the "motherboard" of your application.
It will be in this file that you will define the configuration of your server and that you will add your different modules.
Now, let's define what our application needs a minimum to work.
In your app.php
file, enter the following code.
require 'vendor/autoload.php';
$app = new \Enginr\Enginr();
$app->get('/', function($req, $res) {
$res->send('Hello world !');
});
$app->listen('127.0.0.1', 3000);
💡 Need some explanations ?
OK. First, we need to instanciate Enginr :
$app = new \Enginr\Enginr();
After that, the minimum to do is to declare our first route. Here, we listening for the root route of our website.
$app->get('/', ...
At the same time, we specify the controller (the action we choose to do) when a user goes to this route ...
$app->get('/', function($req, $res) {
$res->send('Hello world !'); // Send 'Hello world !' to the client
});
Now, your application need an host and a port to listening connections ...
$app->listen('127.0.0.1', 3000);
For our application to work, we need to start it.
To do this, let's go to our terminal (to the root location of our project), then launch the application.
php app.php
Open our favorite browser, and enter the host:port of our app. Normaly, we get a great :
Hello world !
That's it ! 🍻