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

Ben Sampson:
Creating a decent Laravel deploy script
Jun 15, 2018 @ 13:45:38

Ben Sampson has posted a quick guide on his site sharing a deployment script for Laravel applications he's found useful.

A good deploy script can save you time and speed up your application, and it only takes a few minutes to set one up.

I have a standard deploy script which I use for almost all of my projects, which I'm going to break down and share with you.

Note: This article assumes that you're using Laravel Forge. If you're not, many of the settings will be applicable, but the setup steps may be a bit different.

He starts with a brief description of what a deployment script is and goes through the functionality he has included in his:

  • switching the site to maintenance mode
  • clearing caches
  • clearing and optimizing routes
  • clearing and optimizing the configuration
  • clearing expired password reset tokens
  • compiling front-end assets

He puts it all together into a single file that's then included in the Forge deployment process. He doesn't provide other instruction as to how to use this script with another deployment method but it can potentially be used anywhere a bash script could.

tagged: laravel deployment script steps laravelforge tutorial

Link: https://sampo.co.uk/blog/creating-a-decent-laravel-deploy-script

Laravel News:
Factory Callbacks and Closure-Based Guards in Laravel
Jun 12, 2018 @ 10:40:36

On the Laravel News site, there's an article posted covering the use of factory callbacks and closures in guards in Laravel. This feature was snuck into a recent release and allows you to provide a bit more dynamic functionality to your models and guards.

Two undocumented (before today anyhow) features were recently added to the Laravel 5.6 documentation, and they are both fantastic!

The post details each of the two new additions starting with the factory callbacks allowing you to perform additional tasks when a model is made or created (code examples included). The guard changes allowing closures and the Auth::viaRequest method to define new guard drivers.

tagged: factory callback model closure guard laravel

Link: https://laravel-news.com/factory-callbacks-closure-based-guards

Set Up an OAuth2 Server Using Passport in Laravel
Jun 08, 2018 @ 11:51:36

The TutsPlus.com site has a new tutorial posted showing you how to create an OAuth2 server with Laravel and the help of the Passport library.

In this article, we’re going to explore how you could set up a fully fledged OAuth2 server in Laravel using the Laravel Passport library. We’ll go through the necessary server configurations along with a real-world example to demonstrate how you could consume OAuth2 APIs.

I assume that you’re familiar with the basic OAuth2 concepts and flow as we’re going to discuss them in the context of Laravel. In fact, the Laravel Passport library makes it pretty easy to quickly set up an OAuth2 server in your application. Thus, other third-party applications are able to consume APIs provided by your application.

The article is then divided up into the steps (code, configuration changes, and commands) required to get the system up and running:

  • Installation of Passport (requires a Laravel app already installed)
  • Changing the User model to add the "remember_token" field
  • Setting up some demo resources
  • Adding the middleware and routes to handle the requests

The tutorial then spends some time showing how to consume OAuth2 APIs and makes use of the Passport "client" to make some sample requests. Finally, it walks you through the whole process of the OAuth2 experience from a user perspective, including some code to manually make the connection from plain PHP.

tagged: tutorial oauth2 server laravel passport client token

Link: https://code.tutsplus.com/tutorials/setup-oauth2-server-using-passport-in-laravel--cms-30576

Matthieu Napoli:
Serverless Laravel
Jun 05, 2018 @ 11:53:36

In a new post to his site Matthieu Napoli shows how, with the help of the Bref package, to run a serverless Laravel instance. This is a continuation from his previous post covering the use of serverless functionality for running plain PHP.

Serverless basically means “Running apps without worrying about servers”. The main difference with a traditional hosting is that you do not maintain the servers and reserve their capacity. They are scaled up or down automatically and you pay only for what you use.

[...] Serverless hosting has the advantages of scaling very well since there are (theoretically) no limits. It can also help optimize costs by avoiding paying for unused server resources. You can read more about advantages and drawbacks here.

After briefly introducing what "serverless" computing is, he dives right into the creation of the first lambda on AWS. He then goes through the process of creating the new Laravel project, installing and configuring Bref, and wrapping the Laravel functionality in its HTTP handler. Next, he shares an example of the serverless.yml configuration, how to set up the file storage (for things like sessions and cache) and configure the application to run correctly on the lambda. The post finishes out with a look at setting up the logging, session handling and routing before finally deploying the app using Bref's own command.

tagged: laravel serverless tutorial bref framework application lambda aws

Link: http://mnapoli.fr/serverless-laravel/

Ben Sampson:
Using enums in Laravel
May 31, 2018 @ 12:26:57

Ben Sampson has written up a tutorial showing you how to use enums in Laravel despite them not being natively supported in PHP.

I'm a big fan of enums. Having recently worked for a company who use C#, where enums are used extensively, I've got used to being able to reach for them and miss them when they're not available. I use them for anything with a limited set of possible options, such as days of the week, the status of an order or, as in the examples below, user type.

[...] Enums aren't natively supported in PHP but an equivalent is fairly easy to achieve using constants on a class. Futhermore I've created a Laravel package called laravel-enum which allows you access helper functions such as listing keys and values, attaching descriptions to values, and validating requests which are expecting enum values.

He then walks you through the Composer installation of the package and provides an example of it in use. In his example he sets up an enum for types of users, defined in a special UserType file (admins, members and paid members). He stores these values in the database and then makes use of the type property on the user to see what type of user they are. He finishes up the post with an example of showing the value of the enum and performing validation on the value with Laravel's built-in validators.

tagged: enum laravel tutorial package laravelenum example

Link: https://sampo.co.uk/blog/using-enums-in-laravel

Ben Sampson:
Masking IDs in URLs using hashids in Laravel
May 29, 2018 @ 12:47:18

In a post to his site Ben Sampson shows how to mask IDs in URLs using hashids in a Laravel-based application. Hashids converts values into strings that can then be decoded back to their original values.

By default a URL generated by a Laravel app will contain the ID of a model like this https://app.name/users/1 where 1 is the ID of the item. Often this is absolutely fine, but sometimes you might want to hide it (or obfuscate it). The two main use cases for this I've come across so far are: Security [and it looks] More professional.

He shows how to use this package to encode and decode ID values in your URLs. He also includes updates to the models, controllers and routing to use route model binding to handle the encode/decode process. He shows how to set up different salts for different data types, setting it on each model and how to correctly bind the functionality in the main route service provider.

tagged: laravel tutorial hashid mask encode decode route model

Link: https://sampo.co.uk/blog/masking-ids-in-urls-using-hash-ids-in-laravel

Scheduling MySQL Backups with Laravel
May 28, 2018 @ 13:19:16

On Pineco.de they've posted a new tutorial showing you how to create a MySQL database backup system using Laravel and a command-line call to mysqldump wrapped in an Artisan command.

A few days ago, I signed in to the wrong database and deleted around 18 000 records that were in use. Not to mention, we had no backup for that data. So after all, I decided to make a little script to dump the database and save it to an SQL file automatically.

But, if you are here for a fully working backup system, you should take a look at <a href="https://github.com/spatie/laravel-backup>this package. We won’t cover more just scheduling database exports and its scheduling.

The post starts with the command that will be executed on the command-line and the code required to "warm up" the console and run the command. He shares the code for this command and walks through it, explaining each part. Next comes the scheduling of the task and, as a bonus, setting up the scheduler to run on the Forge service.

tagged: schedule mysql backup laravel mysqldump tutorial

Link: https://pineco.de/scheduling-mysql-backups-with-laravel/

Christoph Rumpel:
How I Built The LaravelQuiz Chatbot With BotMan and Laravel
May 28, 2018 @ 09:09:14

Christoph Rumpel, lead developer on the BotMan project has a new post to his site walking you through the process and code he used to create a LaravelQuiz chatbot using a combination of the library and Laravel.

Ever wondered how well you know your favorite PHP framework Laravel? Give the LaravelQuiz Chatbot a try, and you will find out. This article is a step by step guide on how I created this chatbot with BotMan and Laravel.

He starts off by introducing the project and what some of the basic features are. Next comes some of the setup, including the botman command line tool and the creation of the BotMan-based project. As the tool uses a connection to Telegram, he also installs the client library for that and sets up the related credentials. Next comes the storage of the questions and answers (migrations and models) and the creation of the QuizConversation class to handle the interactions. There's also code examples for showing the current quiz status, making a "high score" list, and a few other extras. All code and command line calls you need are included in the post.

tagged: laravel quiz botman chatbot tutorial question highscore answer

Link: https://christoph-rumpel.com/2018/05/how-i-built-the-laravelquiz-chatbot-with-botman-and-laravel

Laravel News:
Five Useful Laravel Blade Directives
May 23, 2018 @ 09:29:53

On the Laravel News site they've shared a list of five useful Blade directives that you may not have known about. Blade is the templating language included with the Laravel framework by default.

We’re going to look at five Laravel Blade directives you can use to simplify your templates, and learn about some convenient directives that make solving specific problems a cinch! If you’re new to the framework, these tips will help you discover the excellent features of Blade, Laravel’s templating engine.

The five on their list provide functionality to:

  1. Check if the user is authenticated
  2. Check if the user is a guest
  3. Include the first view if it exists or includes the second if it doesn’t
  4. Include a view based on a condition
  5. Include a view if it exists

Each one comes with example code showing it in use and a bit of explanation behind its use.

tagged: laravel directive blade template top5 list

Link: https://laravel-news.com/five-useful-laravel-blade-directives

Zend Framework Blog:
Zend Framework/Homestead Integration
May 18, 2018 @ 12:48:26

On the Zend Framework blog, project lead Matthew Weier O'Phinney has posted a tutorial showing you how to set up a Homestead instance for Zend Framework thanks to a few helpful submissions from the ZF project.

Last year, we tagged: zendframework homestead integration tutorial laravel apigility zendexpressive

Link: https://framework.zend.com/blog/2018-05-16-homestead.html