I would like to share few VSCode snippets which I found very useful.
Snippets in Visual Studio Code - Create your own snippets
Route declaration snippet
"slim4 skeleton route": {
"prefix": [
"$app->get",
"$group->get"
],
"body": [
"// Route for /$1",
"\\$${2|app,group|}->${3|get,post,put,patch,delete|}('/$1', \\App\\Action\\\\$4::class)->setName('${1/[^A-z^0-9^-]/:downcase/g}');"
],
"description": "Route declaration for Slim4 skeleton"
},
Generates line like:
// Route for /
$app->get('/', \App\Action\::class)->setName('');
Action Controller snippet
"slim4 skeleton action class": {
"prefix": "class",
"body": [
"namespace App\\Action\\\\${TM_DIRECTORY/^.+\\/(.*)$/$1/};",
"",
"use App\\Responder\\Responder;",
"use Psr\\Http\\Message\\ResponseInterface;",
"use Psr\\Http\\Message\\ServerRequestInterface;",
"",
"/**",
" * Action.",
" */",
"final class $TM_FILENAME_BASE",
"{",
" /**",
" * @var Responder",
" */",
" private \\$responder;",
"",
" /**",
" * The constructor.",
" *",
" * @param Responder \\$responder The responder",
" */",
" public function __construct(Responder \\$responder)",
" {",
" \\$this->responder = \\$responder;",
" }",
"",
" /**",
" * Action.",
" *",
" * @param ServerRequestInterface \\$request The request",
" * @param ResponseInterface \\$response The response",
" *",
" * @return ResponseInterface The response",
" */",
" public function __invoke(ServerRequestInterface \\$request, ResponseInterface \\$response): ResponseInterface",
" {",
" return \\$this->responder->withTemplate(\\$response, '${TM_DIRECTORY/^.+\\/(.*)$/${1:/downcase}/}/$0.twig');",
" }",
"}",
""
],
"description": "Slim4 skeleton action class"
},
Generates file like:
<?php
namespace App\Action\Home;
use App\Responder\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Action.
*/
final class HelloWorld
{
/**
* @var Responder
*/
private $responder;
/**
* The constructor.
*
* @param Responder $responder The responder
*/
public function __construct(Responder $responder)
{
$this->responder = $responder;
}
/**
* Action.
*
* @param ServerRequestInterface $request The request
* @param ResponseInterface $response The response
*
* @return ResponseInterface The response
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->responder->withTemplate($response, 'home/.twig');
}
}
Twig template snippet
"slim4 skeleton Twig template": {
"prefix": "extends",
"body": [
"{% extends \"layout/${1|layout-empty,layout|}.twig\" %}",
"",
"{% block css %}",
" {% webpack_entry_css '${TM_DIRECTORY/^.+\\/(.*)$/$1/}/$TM_FILENAME_BASE' %}",
"{% endblock %}",
"",
"{% block js %}",
" {% webpack_entry_js '${TM_DIRECTORY/^.+\\/(.*)$/$1/}/$TM_FILENAME_BASE' %}",
"{% endblock %}",
"",
"{% block content %}",
"",
" <div class=\"container\">",
" $0",
" </div>",
"",
"{% endblock %}",
""
],
"description": "slim4 skeleton Twig template"
},
Generates file like:
{% extends "layout/layout-empty.twig" %}
{% block css %}
{% webpack_entry_css 'home/hello-world' %}
{% endblock %}
{% block js %}
{% webpack_entry_js 'home/hello-world' %}
{% endblock %}
{% block content %}
<div class="container">
</div>
{% endblock %}