Lemon Documentation
Get Started
Lemon is Micro Framework which means that you can build full website app in one file, which is great for beginners.
More about Lemon
Version: 1.3.4
Changelog
Installation
Installation is provided by composer
composer create-project lemon_framework/lemon:dev-master project-name
Lemon won't build your project, only download framework. You need to make file index.php that will be main file of your project and then run using
php -S localhost:port
/project_folder
/lemon
/composer.json
/index.php #here starts your app
Quickstart
This is minimal app built using Lemon:
require "lemon/framework.php";
Route::get("/relative/(.+)/", function($var)
{
echo $var;
});
Route::any("/", function()
{
echo "hi";
});
Route::handler(404, function()
{
echo "404";
});
Route::execute();
Framework reference
Lemon from version 1.0.0 provides basic actions like routing or template system.
On start of every app must be require "leomon/framework.php";
Routing
Routing in Lemon is based on class
Route
and its inspired by Laravel's routing. This class has 3 main static methods to create routes.
The methods are:
- ::get - creates route that allows only method GET
- ::post - creates route that allows only method POST
- ::any - creates route that allows only methods GET and POST
Note: If you will send request that is not allowed by route it will throw response 400.
All those methods has 2 arguments
Path
and
Action
- Path is way to get to the route.
- Action is function that is called when user visits the page.
Class route also provides to run your app. To run just type Route::execute() on the end of the code.
Without this your app won't work.
Dynamic routes
Lemon provides you to make dynamic routes, to make dynamic part, just put
(.*)
and in your route output function argument.
Route::get('/dynamic/(.+)', function ($arg)
{
echo $arg;
});
Example
require "leomon/framework.php";
Route::get('/get', function()
{
echo 'You can only get here!';
});
Route::post('/post', function()
{
echo 'You can only post here!';
});
Route::any('/any', function()
{
echo 'You can get and post here!';
});
Route::execute();
Template system
Template system is way to run html from external file with Cross Site Scripting prevention.
Since version 1.2.0 Lemon has custom template syntax inspired byJinjaa.
To start with templates, just make folder
views
in your project folder and every new template add into this folder as
template_name.lemon.php
.
Your folder should look like this:
/project_folder
/lemon
/composer.json
/views #Here are all the templates
/home.lemon.php #Example template
/index.php #main php file with all the routes
To view template just use function
view(filename, params)
In this function don't use the
.lemon.php
To add variables to template add as second parameter one of those:
- compact("var", "var2",...)
- ["var_name" => "var_value"]
Template syntax
{{ $var }} Shows variable defined in view function
Foreach
{% foreach($var as $var): %}
loops everything that is here
{% endfor; %}
If
{% if (condition) %}
{% else if (condition): %}
{% else: %}
{% endif; %}
With
{%%}
can be provided any php action.
Example
//index.php
Route::get('/' function()
{
$stuff = ['php', 'vim', 'lemon'];
view('awesome_view', compact("stuff"));
});
//views/awesome_view.lemon.php
Cool stuff
{% foreach ($stuff as $item) %}
{{ $item }}
{% endfor %}
Error handling
Class Route has also static method
::handler
which provides you to make custom error handling pages.
This class has 2 arguments: (int) Error and function that is called when the error occurs.
Since version 1.1.0 Lemon supports those errors:
- 404 - Not Found
- 400 - Bad Request
- 500 - Internal Server Error (Is called only when wrong template is used or when there is error in route callback)
If you won't use custom error handlers, Lemon will throw basic error.
Examples
Route::handler(400, function ()
{
echo "Bad request on page".$_SERVER["REQUEST_URI"];
});
Session
Class Session provides you to make session actions through static methods:
- ::setName(string name) - sets name of the session.
- ::start - starts the session.
To make it work, you need to put it on the top of your app.
CSRF protection
Lemon provides CSRF protection by class CSRF:
- ::setToken - creates unique token to every page visitor, should be on the top of the page
- ::getToken - gets you token (made for template @csrf)
- ::check - check you if valid csrf token was send - should be on the top of your route
To make it work, you need to start
session
If you enable csrf protection in your route by
CSRF::check()
you need to add
@csrf
to every post form in your template.
Example
//index.php
<?php
require "lemon/framework.php";
Session::start();
CSRF::setToken();
Route::any("/", function() {
CSRF::check();
view("template");
});
Route::execute();
?>
//view/template.lemon.php
Utils
Here are other functions that may help you building cool stuff.
Redirect
Function
redirect($link)
redirects user to specified URL.
Jsonify
Function
jsonify($array)
makes json from array that can be fetched by request.
Console
Function
console($message, $color)
sends message to php console. It's mainly debug tool
Podvodnik
Function
isUserPodvodnik($user)
returns whenever user is podvodnik.
Lemon micro framework built by Ten Majkl->
GitHub