Lightweight example project of a backend and frontend done with the SLIM 4 Micro-Framework.
This can be used as a base when creating new projects or just serve as inspiration.
Feel free to contribute, re-use, change or basically do whatever you want with this code since it is under the MIT licence. If it can help you in any way, that makes me happy.
Documentation can be found here.
Functionalities:
- Authentication (registration and login)
- User management for a user with ‘admin’ role
- Post creation for all user
- Own post management (edit and delete)
- All user can see all posts
Technologies
Frontend
Languages & libraries
- Mainly Vanilla JS to be as “native” as possible and since E6 JavaScript supports a lot
- Avoiding the use of jQuery but rather add the needed missing components specifically one by one and then bundle it into 1 static file
Template renderer
- Moved from twig to slimphp/PHP-View
- + Native PHP syntax
- + Text translation easier
- + Much more lightweight
- – I have to take care of XSS attack protection by escaping manually
Asset management
After talking with Odan (comment section) I will do the following:
- Link application specific resources directly in template (which are located under
public/assets/*
) - Not using any PHP asset library (like symfony/asset or odan/twig-assets)
- If during the development of a larger project many libraries are being used, I will
- Install webpack and use it to download and compile/bundle my external dependencies (like jQuery, Bootstrap, etc..) into a single JS file.
- Link this static file in my global layout template(s). For this I don’t need an asset function.
- And then I can update my external dependencies with
npx webpack --mode=production
But I think most smaller projects won’t need enough to justify it. Thats why I left it out in this example-project
Backend
- Framework: slimphp/Slim
- PSR-3 logger Monolog
- PSR-11 Dependency injection container: PHP-DI
- Sessions and Flash messages: For now I use the odan/session library because it is so lightweight and has both session and flash. I think its definitely value added to the project and better than something I would do.
If in the future I need something more I’ll consider Symfony Sessions (doc) - Database access: CakePHP Query Builder
Structure
-- app // contains configuration files
-- public
-- assets // images, videos, audio files
-- resources
-- schema // database table creation schema
-- src
-- Application // top layer, contains action classes
-- Domain // includes business logic / service classes
-- Infrastrucutre // database access / manipulation
-- templates
-- user // js, css, html file about users
-- post // js, css, html file about posts
-- tests
-- Application // application testing
-- Domain // service testing
-- Infrastructure // repository testing
Licence
The MIT Licence (MIT). Please see Licence File for more information.