2017 © Pedro Peláez
 

project chubby

An application template for Slim 3

image

a3gz/chubby

An application template for Slim 3

  • Sunday, October 1, 2017
  • by a3gz
  • Repository
  • 2 Watchers
  • 1 Stars
  • 70 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Chubby

Adding some fat to Slim Framework., (*1)

Chubby provides a working Slim application template around which additional fat is added in two ways: (1) useful classes under the fat directory and (2) as modules., (*2)

The first of these modules is Chubby View, a renderer that proposes a very convenient way of organizing code., (*3)

Install via Composer

Go to the directory where you want the new application to sit, then create the project:, (*4)

composer create-project a3gz/chubby -s dev

This will create a new project called chubby., (*5)

Once the project has been created you can safely delete the chubby/composer.json file and the chubby/vendor directory. Don't delete the file chubby/private/composer.json thought, this one is wher you add your dependencies., (*6)

Chubby needs all required dependencies to sit on the private directory:, (*7)

> cd chubby/private
> composer install

Finally go to your browser and request:, (*8)

.../chubby/hello/world

It's very unlikely that you'll want your application to be called chubby, so you may want to rename that directory., (*9)

It is also possible to do this when you create the project:, (*10)

composer create-project a3gz/chubby my-app -s dev

Now your application is in ./my-app., (*11)

Running with Docker Compose

docker-compose up [-d]

The provided docker-compose.yml maps to the host's 9999 port so you should be able to see the site in the following local address:, (*12)

http://localhost:9999

Console request

php console.php path/to/resource

Why Chubby at all?

Chubby is a working application template that offers one possible way to organize a Slim application., (*13)

Around that idea Chubby sets the foundations to split the application files in a way that the code can be placed outside the public_html directory, among other things., (*14)

Configuration

Chubby assumes the existence of a private/app/config directory containing two directories: config/container and config/settings. Under config/container there must extist at least one file called main.php. This file should return an associateive array with settings that will be injected in the container., (*15)

Optionally we can inject additional dependencies by adding more files inside the config/container directory. Each file must return one dependency. Take the provided logger.php for instance:, (*16)

return function ($c) {
  $time = time();
  $year = date('Y', $time);
  $month = date('m', $time);
  $day = date('d', $time);
  $hour = date('H', $time);
  $baseDir = \Fat\Helpers\Path::makePrivatePath('/logs'
    . '/' . $year
    . '/' . $month
    . '/' . $day
  );
  if (!is_dir($baseDir)) {
    mkdir($baseDir, 0777, true);
  }
  $fileName = "{$year}m{$month}d{$day}h{$hour}.log";
  $logFileName = "{$baseDir}/{$fileName}";

  $logger = new \Monolog\Logger($appName);
  $file_handler = new \Monolog\Handler\StreamHandler($logFileName);
  $logger->pushHandler($file_handler);
  return $logger;
};

Chubby will inject the dependency in the container under the same name as the file, in this case: logger: $container['logger']., (*17)

Slim 4

Chubby version ^3 depends on Slim 4.3.0 to keep PHP requirement down to PHP 7.1. If a higher version of PHP is available, changing Slim dependency version to ^4 should work since we are still in the same major version... but I haven't tried this yet., (*18)

This version is more opinionated that Chubby ^1 because some of the things that were taken care of by Slim are now under our control. The new directory src/fat contains classes that help with:, (*19)

  • Initiating the App.
  • Adding an error handler.
  • Solving HttpNotFoundException in a way that makes it easy to customize by simply editing config.php.
  • Bringing back Slim3's Environment::mock() to make console requests possible by mocking an HTTP request.

Hooks

Version 3 includes hooks and plugins support. Plugins are expected to extist under private/app/plugins. Each plugin must have a file called the same as the plugin's directory. This file must include a header section with at least one entry: type which value must be Plugin for the plugins loader to recognize it as such. Check the demo plugin to see how it works., (*20)

Built-in hooks

Name Description
chubby_fallback_theme The name of the fallback theme. If this filter isn't implemented, the falback theme name is default.
chubby_theme Name of the applied theme. Fallbacks to the fallback theme.

Themes

Version 3 includes themes support. Themes can be customized in two levels: styles and views. We can create different stylesheets to change a Web site theme and use the default views. A step further would be to also create custom views for each theme., (*21)

In most situations creating stylesheets would be enough but if we also want to change the layout of a view then we need to create specific views for each theme. Note that we only need to create theme specific views for the views we want to override. If we don't override a view, the default one is automatically used., (*22)

Check the demo to see hot this works., (*23)

Contact the author

I would welcome comments, suggestions and brainstorming-like ideas., (*24)

alejandro@roetal.com, (*25)

The Versions

01/10 2017

dev-master

9999999-dev

An application template for Slim 3

  Sources   Download

MIT

The Requires

 

framework php application template chubby slim