Giter Site home page Giter Site logo

slim3-skeleton's Introduction

Slim 3 Skeleton

This is a simple skeleton project for Slim 3 that includes Twig, Flash messages and Monolog.

Create your project:

$ composer create-project --no-interaction --stability=dev akrabat/slim3-skeleton my-app

Run it:

  1. $ cd my-app
  2. $ php -S 0.0.0.0:8888 -t public public/index.php
  3. Browse to http://localhost:8888

Key directories

  • app: Application code
  • app/src: All class files within the App namespace
  • app/templates: Twig template files
  • cache/twig: Twig's Autocreated cache files
  • log: Log files
  • public: Webserver root
  • vendor: Composer dependencies

Key files

  • public/index.php: Entry point to application
  • app/settings.php: Configuration
  • app/dependencies.php: Services for Pimple
  • app/middleware.php: Application middleware
  • app/routes.php: All application routes are here
  • app/src/Action/HomeAction.php: Action class for the home page
  • app/templates/home.twig: Twig template file for the home page

slim3-skeleton's People

Contributors

akrabat avatar antnee avatar dayofr avatar icharlie avatar jeremykendall avatar mathmarques avatar msheakoski avatar needcaffeine avatar stormwild avatar tjoosten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slim3-skeleton's Issues

How to Unittest?

If there is a best practice to unit test a Slim application with PHPunit it would be nice to have some examples on how to write such tests.

Middleware https://github.com/akrabat/rka-scheme-and-host-detection-middleware not affected

I try to use https://github.com/akrabat/rka-scheme-and-host-detection-middleware to prevent "Mixed content" error. After set X-Forwarded-Proto:https header in controller everything ok: $request->getUri()->getScheme(); return "https".

But here it's not working:

$view->addExtension(new Slim\Views\TwigExtension($c->get('router'), $c->get('request')->getUri()));

$c->get('request')->getUri()->getScheme(); return "http" :(

Full DI container to HomeAction __construct?

Hello, Dear Rob.

Thank you for great code!

Is it possible to put full DI container to HomeAction construct method?

slim3-skeleton/app/src/Action/HomeAction.php:

    public function __construct(Twig $view, LoggerInterface $logger)
    {
        $this->view = $view;
        $this->logger = $logger;
    }

change to something like this:

    public function __construct(ContainerInterface $container)
    {
        $this->view = $container['view'];
        $this->logger = $container['logger'];
        $this->db = $container['db'];
        $this->flash = $container['flash'];
    }

In order to have access to full list of dependencies from class?
Or it's a bad idea?

If I need a logger, view, flash, database mapper etc in one place.

Request/Response objects in example action are incorrectly type hinted

Rob,

I'm not sure if it's deliberate or not, but I noticed that the example action suggests that the Request and Response objects are PSR-7, but they're actually Slim\Http\Request and Slim\Http\Response respectively.

use Psr\Http\Message\ServerRequestInterface as Request;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

should be

use Slim\Http\Request;
use Slim\Http\Response;

The documentation states:

Because this is a common task, Slim’s built-in PSR 7 implementation also provides these proprietary methods that return true or false

So it's not technically a PSR-7 implementation. I'll submit a PR in case this isn't deliberate :)

Syntax for calling a method in a route

The syntax: 'App\Action\HomeAction:dispatch'

Where can I learn more about this? I have never encountered it before outside of a Slim3 context. Is this something unique to Slim/Pimple/FastRoute or this a feature in PHP5 that has eluded me?

I feel silly, but I would very much like to understand this seemingly simple syntactical detail.

Thank you very much for this skeleton. It is proving to be extremely helpful.

Twig issue - Catchable fatal error: Argument 1 passed to Twig_Filter

Hi,
I use php 5.6 and get error in Twig when try to render page.

Catchable fatal error: Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given, called in system/vendor/twig/twig/lib/Twig/Extension/Core.php on line 139 and defined in system/vendor/twig/twig/lib/Twig/Filter.php on line 35

new Action Class DI error

I create a new Class in app/src/Action which namespace is App\Action too. And use exactly the same constructor with HomeAction .
I use it in route file but got next error:
Catchable fatal error: Argument 1 passed to App\Action\ProductAction::__construct() must be an instance of Slim\Views\Twig, instance of Slim\Container given, called in /Users/tinker010/zhengjie/kafe/vendor/slim/slim/Slim/CallableResolver.php on line 64 and defined in /Users/tinker010/zhengjie/kafe/app/src/Action/ProductAction.php on line 21

Don't know why, here is my New Class:

view = $view; $this->logger = $logger; } public function productAddPage(Request $request, Response $response, $args) { return $this->view->render($response, 'product.html'); } ``` }

Lacking default timezone for PHP5.5

Running this skeleton on a PHP5.5 install, user will see an error related to the lack of default timezone (used by the log).

Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in my-app/vendor/monolog/monolog/src/Monolog/Logger.php on line 272

This can be easily fixed by adding a date_default_timezone_set('Etc/UTC'); to index.php.

Remove scheme from url

To avoid mixed content issues when running slim3-skeleton from https, remove scheme from url to google fonts.

<link href='//fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>

in app/templates/home.twig

Try Catch Exceptions

Hello,

Using your example, what's the better way to throw exceptions from within HomeAction class?

Common dependencies

As per discussion in this Tweet stream.

Defining a new factory for every controller separately seems wasteful, especially when most might share some, if not all dependencies.

What would be good approaches to handling this? Originally, I thought about something like this:

screenshot 2015-08-05 00 08 56

And then extending the get and has methods as necessary.
But this seems a bit clumsy and too much work for such a simple feature - especially considering a new offsetExists would need to be added into the mix, too, and some other things.

Suggestions?

Question regarding Actions

This is my first time using Slim and have a question. In looking at your config, would you suggest putting the Actions within another directory for example

  • app
    • src
      • Actions
        • User
          • CreateAction.php
          • UpdateAction.php
          • DeleteAction.php
        • Post
          • CreateAction.php

or do you have a different suggestion.

Here is how I currently have my project structure

  • app
    • src
      • Actions
        • BaseAction.php
        • Users
          • AllActions.php
          • CreateAction.php
          • ReadAction.php
          • UpdateAction.php
          • DeleteAction.php
        • Posts
          • AllActions.php
          • CreateAction.php
          • etc....
      • Models
        • User.php
        • Post.php

I find this structure will keep my actions contained and small. AllActions is the final class which extends the baseclass. I use traits for CRUD and use them in the Class.

Add base_url() in resource paths

To allow loading resources when running app from a sub folder, it may be appropriate to include reference to base_url() in resource paths.

<link href='{{ base_url() }}/css/style.css' rel='stylesheet' type='text/css'>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.