Laravel like slimframework skeleton
Slim micro framework has popular and simple basic PHP framework and is amazing to develop web applications with more performance, clean, simple and optimized, on other way Laravel framework
has configured and very powerful feature like template engine, Eloquent for database interactions, cli, events and etc, and that’s amazing too,
so slim skeleton configured and structured in slim base with some Laravel features and structure,
skeleton using slimframework for base and twig for template engine, monolog for logger, Laravel Eloquent for database interactions, laravel mix for js/css operations and etc.
If you worked with Laravel, then the skeleton structure is familiar to you.
It’s recommended that you use Composer to install Slim-skeleton.
So, before using Skeleton, make sure you have Composer installed on your machine.
$ composer create-project sadegh-pm/slim-skeleton app-name
env.yaml
in root of projectnpm install
and compile asset(js/sass), npm dev
or npm prod
for production build. see package.json for more options. (You must installed node js in your machine)php cli serve
or composer start
composer test
app
:
Command
: Define your custom command for cli Controller
: Your app controllersDependency/dependency.php
: Define your app dependencyEvents
: Your app eventsKernel
: App startup process including twig,logger,database,error-handlers dependency and helpersListeners
: Your app listenerMiddleware/middleware.php
: Define your app middlewareObservers
: Your app Observers
register_observer.php
: Register app observers App model define in root of app directory
config
: Configuration files, define your config file and then access their with config
helperpublic
: Public directoryresources
: App resource directoryjs
: App js fileslang
: App localization language directories and filessass
: App css filesviews
: App views directories and filesroutes
:web.php
Define app web routesapi.php
Define app api routesstorage
:app
Use for upload and other purposeframework
: Used for framework usage like twig cache directorylog/app.log
: App log filetests
: Test directoryenv.yaml
Environment configuration fileIf you need develop in local server you may use Skeleton core command or Composer command.
This commands will start a development server at http://localhost:8000
// Skeleton Command
$ php cli serve
// Composer Command
$ composer start
All of the configuration files are stored in the config directory.
you may use the get_env helper to retrieve values from these variables in your configuration files but if you need value
in app or twig just use config helper like next section.
'debug' => get_env('app.debug', true)
Accessing Configuration Values:
You may easily access your configuration values using the global
config helper function from anywhere in your application.
$value = config('app.timezone');
Skeleton use PSR-6 Cache and provides redis and file cache driver you can define in env.yaml.
The cache configuration is located at config/cache.php
.
In this file you may specify which cache driver you would like to be used by default throughout your application.
You can see all cache method in app/Kernel/Util/Cache.php
or in test file for cache in tests/Component/CacheTest.php
Skeleton use Laravel validation and with validator helper you can use it for
validate request data. (You can see full documentation in Laravel validation docs)
You can see simple example for validation in tests/Component/ValidatorTest.php
The slim skeleton use Silly CLI that just an implementation over the Symfony Console.
You can read the Silly CLI documentation or Symfony documentation to learn everything you can do with it.
Command file of config directory has two sections command, For user custom command and skeleton core command. (HelloWorldCommand created for example)
For define new custom command:
app/Command
directoryDefine it in config/command.php
in user section
Core command:
You can see core command file in app/Kernel/Command
and use that for create custom command.
Events provides a simple observer implementation, allowing you to subscribe and listen for various events that occur in your application.
app/Events
directoryapp/Listeners
config/listen.php
fireEvent
helper for call eventsLocalization features provide a convenient way to retrieve strings in various languages,
allowing you to easily support multiple languages within your application.
Language strings are stored in files within the resources/lang
directory.
Within this directory there should be a subdirectory for each language supported by the application and
all language files return an array of key value strings.
You can use trans
or __
(2 underscore) helpers in app and twig template for get value from local file.
Skeleton comes with some helpers. For more information see app/Kernel/helper.php
:
route($name,$params)
(#route-anchors) In php and twig Build the path for a named route including the base pathconfig($key,$default)
In php and twig get a configuration setting using a simple or nested key. Nested keys are similar to JSON paths that use the dot dot notation.asset($file)
In php and twig get resource urlinput($key,$default)
In php and twig Fetch parameter value from query string or retrieve any parameters provided in the request bodytable($tableName)
In php and twig Begin a fluent query against a database table using eloquent query buildertrans($key)
In php and twig get value from locale filesSkeleton use Laravel eloquent and database system, For more information about eloquent you can see
Laravel eloquent documentations, you can usetable
helper for interacting with database too and for more information see Laravel query builder documentations
If you are listening for many events on model,
you may use observers to group all of your listeners into a single class.
Observers classes have method names which reflect the Eloquent events you wish to listen for.
Each of these methods receives the model as their only argument.
app/Observers
app/Observers/register_observer.php
Skeleton use powerful filesystem
you can see more information in Filesystem documentations
or refer to Laravel file storage documentations.
For access to storage file in public directory need to create symlink by below command:
$ php cli storage:link
The below section is test for how to use storage (tests/Component/StorageTest.php
)
public function testSaveFileOnStorage()
{
/** @var Filesystem $storage */
$storage = dependency(Filesystem::class);
$testFile = "test_file.txt";
$content = uniqid('random_text');
$creationStat = $storage->put($testFile, $content);
$this->assertTrue($creationStat);
$this->assertTrue($storage->get($testFile)->exists());
$this->assertEquals($content, $storage->read($testFile));
$this->assertTrue($storage->delete($testFile));
}
Skeleton use twig template engine that is a modern template engine for PHP and:
Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.
For more information about twig you can see Twig page.
resources/views
directory.The tests/Functional
include sample tests.phpunit used for testing. Define your functional test in this directory.
use composer test
for run tests.
The Slim-skeleton framework is open-sourced software licensed under the MIT license.