Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Allen:
Handing JSON data errors in Slim 3
Nov 16, 2016 @ 15:12:01

For the Slim framework users out there Rob Allen has a quick new post to his site showing an easy way to handle JSON data errors in a Slim 3 application (besides just a silent failure).

When you send JSON data into a Slim Framework application with a content-type of application/json, then Slim will decode it for you if you use getParsedBody(). [...] If there's an error however, you get [an empty response]. If you care about this, you can use json_last_error_msg() and return an error.

He includes a code example of using this function on an endpoint, providing more information on why the JSON parsing failed. He also includes a "bonus" that makes use of the jsonlint library to inspect the JSON message and provide back a more human-readable message letting the user know what exactly was wrong with the JSON they provided.

tagged: json error slim slimframework jsonlint tutorial jsonlasterrormsg

Link: https://akrabat.com/handing-json-data-errors-in-slim-3/

CloudWays Blog:
Josh Lockhart Discusses Slim Framework And Photography
Jul 29, 2016 @ 17:30:58

The Cloudways blog has posted their latest interview with a member of the PHP community that has made major contributions. This time they talk with Josh Lockhart, the creator of the popular Slim framework and contributor to the PHP: The Right Way guide.

Josh Lockhart is the creator of Slim Micro Framework for PHP, a well known framework for writing powerful API and web apps. He is also managing PHP The Right Way, a project that provides best practices and quality information about PHP in one place. He works as a senior web developer and special project director at New Media Campaigns.

In this interview Josh talks about his experience with PHP. He also describes how he came up with the Slim Framework and offers great advice to PHP newbies.

In the interview Josh answers questions about:

  • his own history and experience in PHP development
  • how he sees PHP has progressed over the years (and where it's heading)
  • why he created the Slim framework and what makes it different
  • the tools/software he uses to do his work

He also shares some recommendations to beginners with the language and suggests some software for those looking at things like content management systems. The interview finishes up with Josh sharing some about his own passion for photography and his own work environment.

tagged: joshlockhart slim slimframework photography interview community

Link: http://www.cloudways.com/blog/josh-lockhart-interview/

Rob Allen:
Slim 3.4.0 now provides PSR-7!
May 09, 2016 @ 14:48:10

Rob Allen has a post to his site announcing the latest release of the Slim Framework - v3.4.0 - and an update that allows for full PSR-7 support, telling Composer that the framework fully supports it now as well.

I've been neglecting Slim's PR queue recently, so this weekend I dedicated a lot of time to merging all the good work that our contributors have done. As a result, I'm delighted to release version 3.4.0! This release has a larger set of changes in it than I would have ideally liked which is a direct consequence of having gone two months between releases rather than one.

One particularly interesting addition that we have a made this release is adding a provide section to our composer.json file. [...] This means that we have informed Composer that Slim provides a valid implementation of the interfaces in psr/http-message-implementation virtual package that defines the PSR-7 interfaces.

This basically means that if you're using other libraries/tools that require a PSR-7 compatible system to work correctly, they'll detect that Slim fully supports it.

tagged: slimframework slim3 psr7 support http message implementation composer

Link: https://akrabat.com/slim-3-4-0-now-provides-psr-7/

Rob Allen:
DI Factories for Slim controllers
Apr 28, 2016 @ 15:38:10

In a recent post to his site Rob Allen shows you how to create dependency injection factories for Slim (v3) controllers.

When using classes for route actions in Slim 3, I recommend using a single class for each route. However you can use a single class for multiple routes. To register a class method to a route you pass a string as the route callable where the class name is separate from method by a colon. Slim will retrieve MyController from the DI container and then call the listAction method using the usual signature. f you don't specify a method, then Slim will see if it treat the class as a callable, so you can implement __invoke() and then register the route.

He shows how to create a factory method as a part of the Slim container for a constructor and inject the correct dependencies. He shows how to register these dependencies as other factory methods inside the DI container and how to pull them back out for injection. He prefers this method as it reduces "hidden dependency" issues and makes the controllers themselves easier to test.

tagged: slim3 slimframework dependency injection container tutorial factory

Link: https://akrabat.com/di-factories-for-slim-controllers/

Rob Allen:
Using Eloquent in Slim Framework
Apr 07, 2016 @ 14:45:31

Rob Allen has a quick post showing how to use Eloquent in a Slim framework application to work with your database queries and functionality.

Eloquent is one of the easier ORMs to get started with, so let's look at how to use and test it within a Slim Framework application. [...] Eloquent is quite a nice implementation of the Active Record pattern. If that meets your needs, then it is easy to use within a Slim Framework application, so go for it!

He walks you through the set up of the Eloquent package via a Composer require and update your Slim configuration with the database settings. He then includes the few lines you'll need to use the "capsule" manager to integrate it with the Slim structure. He also talks about testing models and shows how to create some simple tests with PHPUnit and a bit of mocking.

tagged: slimframework eloquent database capsule composer tutorial

Link: https://akrabat.com/using-testing-eloquent-in-slim-framework/

Joe Ferguson:
Adding Homestead to a SlimPHP App
Mar 22, 2016 @ 16:26:09

Joe Ferguson has a quick post to his site today showing you how to use a Homestead VM to run a Slim 3 application with just a few quick commands.

I’ve been a fan of SlimPHP for a really long time. Recently I started using it for a site that I do freelancing development for and wanted to share a quick solution to local development. It’s no secret that I love Laravel’s Homestead vagrant environment. Using the per-project installation method, you are only three commands away from running a SlimPHP skeleton application in a local Homestead VM.

The remainder of the post shows the commands to:

  • create a new Slim application based on the Skeleton
  • Setting up Homestead
  • And creating a new instance to host the Slim application

With a quick vagrant up your virtual machine is ready to go with the application ready to run.

tagged: homestead virtualmachine slim3 slimframework tutorial install composer

Link: https://www.joeferguson.me/adding-homestead-to-a-slimphp-app/

Rob Allen:
Configuration in Slim Framework
Mar 16, 2016 @ 17:30:10

If you're a Slim framework user you should check out the latest post on Rob Allen's site covering all things configuration in using the framework and it's simple configuration handling.

Configuration in Slim Framework is nice and simple: the App's constructor takes a configuration array for the DI container.

He shows how to pass in the configuration as a optional constructor parameter on the main application, including a settings value containing some of the common options. These include the displayErrorDetails flag to show/hide detailed error messages and a logger setup (in his example Monolog). He also shows how to:

  • get settings from the configuration
  • use a separate file for the configuration
  • using a .env configuration file
  • combining multiple configuration files

He includes code examples for each of these cases as well as a method for using a non-array structure (like YAML or XML) via the ZendConfig component.

tagged: slim slim3 slimframework configuration option tutorial settings

Link: https://akrabat.com/configuration-in-slim-framework/

Rob Allen:
Testing Slim Framework actions
Mar 14, 2016 @ 15:45:52

Rob Allen has a quick post to his site showing you how to test Slim actions using PHPUnit and some simple pieces of the Slim framework itself to set up the needed environment.

To test a Slim Framework action, you need a request and a response object and mock whatever is in the action. This is one way to do this.

He gives an example of a simple endpoint that just returns a JSON string. He shows the code for this endpoint and how it registers with the application for an /echo route. He then gets in to the testing on the route's matching class, making an instance of the Request class and an Environment for it to work in. He ends the post by sharing teh code to pull all of these pieces together in a simple PHPUnit test that uses the assertSame assertion to verify the JSON response output.

tagged: slimframework action unittest phpunit testing tutorial request environment

Link: https://akrabat.com/testing-slim-framework-actions/

PHP-DI:
Introducing the PHP-DI bridge for Slim
Mar 11, 2016 @ 16:15:24

In this new article on the PHP-DI project site they've shared one of the latest tools they've created to make it easier to integrate PHP-DI with the latest version of the Slim microframework (v3).

Slim 3 was released 3 months ago and it was significant. It is one of the first frameworks to integrate the latest standards and concepts in its core.

[...] While PSR-7 and middlewares are very interesting, what's more useful for us is the use of container-interop. That means Slim 3 can work with any dependency injection container. It is very easy to replace the default container (Pimple) with PHP-DI, but today we are releasing a "PHP-DI - Slim" bridge that goes a little further.

They then show how to use their new bridge to create controllers as services in the container and pass them parameters. The post ends with the instructions on getting the PHP-DI bridge installed and how to use it to replace the default AppSlim creation of your application.

tagged: phpdi dependency injection container slim microframework slimframework v3 bridge

Link: http://php-di.org/news/18-slim-bridge-released.html

Johannes Siipola:
How to use Eloquent ORM migrations outside Laravel
Jan 20, 2016 @ 17:55:10

On his site Johannes Siipola has posted a tutorial showing how to use the Eloquent ORM outside of Laravel, more specifically the migration functionality to manage database table creation/updates/deletions.

Laravel's Eloquent is one of the most fluent and expressive ORM's available for PHP. However, unlike some of its competitors like Doctrine or Propel, it's highly tight to the Laravel ecosystem in some ways. It's still possible to use Eloquent outside Laravel with a bit of work.

[...] If you use Eloquent outside Laravel, you will be missing one of it's best features: database migrations. [...] There has been some effort in order to use Laravel migrations outside the framework, but it hasn't been possible because the migrations require a complete instance of Laravel to run. In this tutorial, we are using framework agnostic Phinx migration library instead.

He then shows how to install the packages needed for the example: Slim framework, the Eloquent ORM and Phinx. He then shows how to set up the database and Phinx configurations and create your first Phinx migration. Inside this migration he makes use of the Eloquent schema builder functionality to create a simple "widgets" table. He ends the post showing you how to create the simple Slim application and work with the model they've created to both save and read data easily.

tagged: eloquent laravel outside orm migration phinx slimframework tutorial

Link: https://siipo.la/blog/how-to-use-eloquent-orm-migrations-outside-laravel


Trending Topics: