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

Matt Stauffer:
Defining console commands via closure in Laravel 5.3
Feb 17, 2017 @ 11:06:37

Matt Stauffer has posted the latest article in his "New Features in Laravel 5.3" series today. In this new tutorial Matt focuses on the creation of console commands - additional functionality you can add in to the pre-existing "artisan" command handling.

Before Laravel 5.3, defining an Artisan console command—something like php artisan sync:dates—required you to create a new class for that command and register it in the Console Kernel. This is fine, but sometimes it feels like overkill for what might end up just being a single line of functional code.

As of Laravel 5.3, you'll notice that there's a new method in the Console/Kernel.php file named commands(), and it loads a new file at routes/console.php. This new "console routes" file allows us to define Artisan console commands with a single Closure instead the prior "define a class then register it in the console Kernel" flow. Much faster, much easier.

In v5.3 you define commands using "routes" along with a simple description using fluent statements. He shows how to add a simple command, one with input and a more streamlined example pulling values directly from the "route" signature.

tagged: laravel console commands closure v53 version tutorial route closure

Link: https://mattstauffer.co/blog/defining-console-commands-via-closure-in-laravel-5-3

Matt Stauffer:
The auth scaffold in Laravel 5.2
Jan 11, 2016 @ 10:06:29

Matt Stauffer has continued his series about some of the new features in the latest release of the Laravel framework (v5.2) with this post looking at the new auth scaffolding it makes available.

If you're like me, many of the applications you build in Laravel have a similar Saas-type framework: user signup, user login, password reset, public sales page, logged-in dashboard, logout route, and a base Bootstrap style for when you're just getting started.

Laravel used to have a scaffold for this out of the box. It disappeared recently, to my great chagrin, but it's now back as an Artisan command: make:auth.

He talks about what all the scaffolding builds out including templates, routes and controllers. He provides examples of some of the generated code and what the output of these simple templates looks like (including a basic Bootstrap layout).

tagged: laravel framework auth scaffold tutorial example login user template controller route

Link: https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2

Matt Stauffer:
Middleware groups in Laravel 5.2
Dec 23, 2015 @ 09:28:31

In the next part of his series spotlighting features in the most recent release of the Laravel framework (5.2), Matt Stauffer continues with a look at middleware groups.

When you are creating a site of any significant size in Laravel, your routes file will often get pretty large. One of the first things I do in a new site is group my routes by logically distinct sections like "admin", "auth", "public". Usually each of these groups get their own set of middleware—admin, for example, gets auth. Maybe the API group gets a different auth middleware, and it might get an API-specific rate limiter or something else.

Laravel 5.2 has introduced something called middleware groups, which are essentially a shortcut to applying a larger group of middleware, using a single key.

He takes his example above and makes an "admin" middleware group that lets you combine individual middlewares into a single callable set. He shows how to update your HttpKernel.php file with the new "auth" group and nest the "web" and "auth" middleware inside. He talks briefly about how this handling has changed from 5.1, pointing out that things without the "web" middleware will not have access to cookies/sessions/CSRF handling. He then includes an example showing how to use this "admin" grouping in your routes, either directly on a route or through a route grouping.

tagged: laravel route middleware group tutorial

Link: https://mattstauffer.co/blog/middleware-groups-in-laravel-5-2

Matt Stauffer:
Implicit route model binding in Laravel 5.2
Dec 18, 2015 @ 09:34:53

Matt Stauffer has continued his series looking at the new features of Laravel v5.2. In this new article he talks about the addition of implicit route model binding. This is a bit more fancy term for the ability to link models to routes and tell Laravel they're related.

If you've never used it, Laravel's route model binding has been around for a while, but Laravel 5.2 is about to make it even easier. [...] In your route service provider, just teach the router: $router->model('shoe', 'AppShoe'); That means, "any time I have a route parameter named shoe, it's an ID representing an instance of AppShoe". [...] In Laravel 5.2, it's even easier to use route model binding. Just typehint a parameter in the route Closure (or your controller method) and name the parameter the same thing as the route parameter.

He includes code examples of the "old way" to do it with model functionality inside the route and the "new way" of linking the model to the route. This allows you to auto-magically have a valid model instance once the route is called. He also shares some of the little known features of this model binding including using closures in bind() calls and changing the "route key" away from the "id" column.

tagged: model route binding laravel feature implicit feature v52

Link: https://mattstauffer.co/blog/implicit-model-binding-in-laravel-5-2

SitePoint PHP Blog:
Build a Superfast PHP Server in Minutes with Icicle
Sep 17, 2015 @ 11:21:44

The SitePoint PHP blog has posted a new tutorial by Christopher Pitt showing you how to build a PHP server "super fast" with the help of the Icicle/http library and some event-driven programming techniques.

Event-based programming is a strange topic for PHP developers. In a language as procedural; events are little more than function calls. Nothing happens between events, and all meaningful code is still blocking.

Languages like JavaScript show us what PHP could be like if event loops were at the center. Some folks have taken these insights and coded them into event loops and HTTP servers. Today we’re going to create an HTTP server, in PHP. We’ll connect it to Apache to serve static files quickly. Everything else will pass through our PHP HTTP server, based on Icicle.

They start off showing you how to configure your Apache server to rewrite the requests (only for non-existent files) to the PHP handler. From there, he helps you get the Icicle/http library installed and create a simple HTTP server with it's included functionality. He shows how to set up routing using the LeagueRoute package and return correct HTTP response codes based on the result of the request. Finally he shows the use of the LeaguePlates library to render more complex views than just plain-text results.

tagged: tutorial http server icicle league plates route

Link: http://www.sitepoint.com/build-a-superfast-php-server-in-minutes-with-icicle/

Anthony Ferrara:
Prefix Trees and Parsers
May 19, 2015 @ 10:13:18

Anthony Ferrara has a new post, following up from his previous look at tries and lexers, continuing along the path to apply what he learned to a HTTP routing system.

In my last post, Tries and Lexers, I talked about an experiment I was doing related to parsing of JavaScript code. By the end of the post I had shifted to wanting to build a HTTP router using the techniques that I learned. Let's continue where we left off...

He starts off with thinking that lexing and parsing the routes out into their respective tokens instead of breaking them up as many do (i.e. splitting on the slashes). He shows the results of this lexing and some parser code to handle these results and turn them into something useful. He did find that the current setup caused a lot of overhead (255 new states per character) so he optimizes the processing with a "default" trie but it was still pretty intensive.

He decided to go a different way at this point, opting for the radix tree structure instead. He includes the implementation of this tree for parsing the routes and his matching lexer updates. Finally he shows how to apply code generation to the results of these changes and how coming back to the "slash splitting" could help...

tagged: lexer parser example prefix tree radixtree route matching slashes

Link: http://blog.ircmaxell.com/2015/05/prefix-trees-and-parsers.html

Rob Allen:
Routing to a controller with Slim
Feb 18, 2015 @ 10:19:08

Rob Allen has a new post today sharing an extension for Slim he's written to make working with controllers easier.

In a couple of projects that I've written using Slim Framework, I've found it beneficial to organise my code into controllers with injected dependencies; probably because that's how I'm used to working with ZF2. To make this easier, I've written an extension to the main Slim class and packaged it into rka-slim-controller which will dynamically instantiate controllers for you for each route.

His library makes it easy to define controller "paths" instead of the typical closures Slim requires to direct the request to a controller file. He gives several example routes, the code that the controller needs and shows how you can inject objects into the constructor of the controller (dependency injection).

tagged: slimframework slim library route controller dependency injection

Link: http://akrabat.com/slim-framework/routing-to-a-controller-with-slim-framework/

SitePoint PHP Blog:
Getting Started with Symfony2 Route Annotations
Nov 06, 2014 @ 09:27:25

On the SitePoint PHP blog today there's a new tutorial posted that gets into the details on one of the powerful (and most used) components of the Symfony framework, the Router, and how to interact with it via annotations. Symfony's route annotations allow you to define functionality at the controller level or via a YAML configuration file.

When you download the Standard Symfony 2 Distribution, it includes an interesting bundle named SensioFrameworkExtraBundle which implements a lot of great stuff, especially the opportunity to use annotations directly within your controllers. The idea behind this article is not to convice developers to embrace this way of doing, but to point the finger at an alternative method to easily configure controllers. Keep in mind that there is no magic recipe, it depends on what you need in each specific scenario. Symfony 2 implements a strong built-in component to manage all the routes of an application: the Routing Component. Basically, a route maps a URL to a controller action.

His example sets up a simple blog and compares the two ways of configuring the routing: one side putting it all in the YAML configuration and the other in the controller docblocks (annotations) themselves. He adds a base page for showing the listing of posts and a single article route, complete with slugs. He also shows how to add in some additional configuration handling to do things like set default values, making things required and enforcing the HTTP method on the request (GET, POST, PUT, etc)

tagged: route annotations introduction docblock tutorial yaml configuration settings

Link: http://www.sitepoint.com/getting-started-symfony2-route-annotations/

Zumba Tech Blog:
Caching CakePHP 2.x routes
Oct 28, 2014 @ 10:47:02

On the Zumba Tech Blog today there's a new post with some helpful hints around caching routes in CakePHP 2.x to help optimize the requests and response time even further.

At Zumba we are continuously looking for optimization in our applications. These optimizations help to reduce the server loads, consequently reducing the number of servers and saving money. Besides that, it gives a better user experience for the end user by serving content faster and in some cases saving on consumer bandwidth (specially for mobile users). This week we profiled our app using Xdebug profiler and we identified the router was responsible for a big part of the request time. [...] In order to optimize the routing time, we started looking at options to optimize our routing process. After some research and deep checking in our codebase as well as CakePHP’s code, we found we could cache the routes easily.

Taking a cue from how FastRoute does their caching, their implementation uses a temporary file with the routes completely resolved and written out for easier handling. Since the routing is relatively static, this method works well and can be much faster than resolving them every time. They talk about some of the work done to optimize their method and some of the issues they came across during the process.

tagged: cakephp framework cache route file resolve

Link: http://tech.zumba.com/2014/10/26/cakephp-caching-routes/

Developer Drive:
Introducing Laravel, part 2
Oct 28, 2013 @ 13:18:51

The Developer Drive blog has posted the second part of their series introducing the Laravel PHP framework. In this new tutorial they build on the basics from part one to briefly discuss controllers and the Eloquent ORM.

In the first part of this introductory mini series we looked at simple routes and views and now we’ll look at how to work with controllers and models , how these two fit in the framework and how to use them.

They explain some of the basics of controllers first including a bit of sample code showing how to output a basic view and add a new route. Following that is a brief look at using the ORM and making a model - a Post - and defining the table it relates to.

tagged: laravel framework introduction series part2 controller route model eloquent orm

Link: http://www.developerdrive.com/2013/10/introducing-laravel-part-2/