Skip to content

Instantly share code, notes, and snippets.

@andy-williams
Last active December 4, 2022 23:07
Show Gist options
  • Save andy-williams/0dd799bad6b5bd2967d36d1c97f532b5 to your computer and use it in GitHub Desktop.
Save andy-williams/0dd799bad6b5bd2967d36d1c97f532b5 to your computer and use it in GitHub Desktop.
Lumen cheat sheet

Lumen / Laravel Cheat sheet

Install Lumen / Laravel

install global installer

composer global require "laravel/lumen-installer"

composer global require "laravel/installer"

start new project

lumen new {project-name}

laravel new {project-name}

start app

php -S localhost:8000 -t public

Config

change config

.env in root dir

access config

$config_key = env('{config-key}')

Routing

Note: Route parameters cannot contain the - character. Use an underscore (_) instead.

Verbs

$app->get($uri, $callback);
$app->post($uri, $callback);
$app->put($uri, $callback);
$app->patch($uri, $callback);
$app->delete($uri, $callback);
$app->options($uri, $callback);

Route Parameters

$app->get('user/{id}, function(Request $req, $id) {
	//
}

Constrained Route Parameters

$app->get('user/{name:[A-Za-z]+}', function ($name) {
    //
});

Named Routes

$app->get('profile', ['as' => 'profile', function (Request $req) {
    //
}]);

$url = route('profile');

Route Groups

Allows a group of routes to share namespaces, prefix and middleware

Middleware

$app->group(['middleware' => 'auth'], function () use ($app) {
    $app->get('/', function ()    {
        // Uses Auth Middleware
    });

    $app->get('user/profile', function () {
        // Uses Auth Middleware
    });
});

Base Route / Prefix

$app->group(['prefix' => 'accounts/{account_id}'], function ($app) {
    $app->get('detail', function ($account_id)  {
        // Matches The accounts/{account_id}/detail URL
    });
});

Middleware

A middleware is a layer that lives between the web request handler (web server) and the router and controller.

request -> middleware -> routing/controller

This allows you to modify the request before being passed to the controller, check for tokens, start a timer before and stop after allowing you to log how long the controller takes etc.

Creating a Middleware

middleware([ App\Http\Middleware\App\Http\Middleware\MyMiddleware::class ]); #### Only Certain Routes $app->routeMiddleware([ 'middleware' => App\Http\Middleware\App\Http\Middleware\MyMiddleware::class ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment