SitePoint PHP9 Hot Tips to Enhance Your Spark Experience (28.9.2016, 16:00 UTC)

A while ago, I wrote about a product I wanted to build, to allow easy remote backups for Pagekit sites. I've been working on it (periodically) since then, and have come across a few interesting bits of advice.

I decided to use Laravel Spark as the foundation for the product, and I thought it would be helpful to share the advice. Whether you're just starting your Spark app, or are in maintenance mode, I think you'll find some of these tips useful!

Laravel Spark Website Splash Screen

1. You don't have to keep all the base files

You may be worried about removing too many of the base files from the standard Spark installation. When I first started, I thought it vital not to change the auth controllers (in app/Http/Controllers/Auth), for fear that it'd break the registration and login system.

Turns out, these files aren't used by Spark. In fact, if you add routes to them, and you try to register/log in, you'll probably just encounter problems. These default auth controllers share the same auth guard (session driver), so logging in through one will make you authenticated through the other.

If, however, you try to register through the non-Spark controllers, your user and team accounts will be missing vital Spark information. It's cleaner and simpler to just delete these auxiliary auth controllers.

If you're unsure, make a full backup. Then you can roll back in case your tests pick up any regressions.

2. Use simple repositories

Spark includes a few simple repositories. These are like static config lists (for countries and other mostly-static data), but they can be loaded through the IoC container. They look like this:

namespace Laravel\Spark\Repositories\Geography;

use Laravel\Spark\Contracts\Repositories\↩
    Geography\CountryRepository as Contract;

class CountryRepository implements Contract
{
    /**
     * {@inheritdoc}
     */
    public function all()
    {
        return [
            'AF' => 'Afghanistan',
            'AX' => 'Åland Islands',
            'AL' => 'Albania',
            // ...snip
            'YE' => 'Yemen',
            'ZM' => 'Zambia',
            'ZW' => 'Zimbabwe',
        ];
    }
}

This is from vendor/bin/laravel/spark/src/Repositories/Geography/CountryRepository.php

We can see instances of this being used in some of the registration views:



This is from resources/views/vendor/spark/auth/register-address.blade.php

I highly recommend you use these repositories for country and state data. I also recommend you use this repository style for your own lists:

namespace App\Repositories;

use DateTimeZone;

class TimezoneRepository
{
    /**
     * @return array
     */
    public function get()
    {
        $identifiers = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

        return array_combine(
            $identifiers,
            array_map(function ($identifier) {
                return str_replace("_", " ", $identifier);
            }, $identifiers)
        );
    }
}

You don't have to make an interface for each repository. In fact, I think that's a bit of an overkill. But I think these tiny repositories are much cleaner and easier to use than the alternatives.

In addition, you can alias these in an application service provider:

Continue reading %9 Hot Tips to Enhance Your Spark Experience%

Link
Nomad PHPRFCs of the Future: Tick Talk (28.9.2016, 14:42 UTC)

Speaker: Cal Evans @calevans Signal handling in PHP is one of those things that not a lot of people understand, so they don’t bother with. However, if you are writing command line scripts, signal handing is your friend. In PHP 7.1, it gets even easier thanks to changes made in the engine. This short video …

The post RFCs of the Future: Tick Talk appeared first on Nomad PHP.

Link
Nomad PHPBuilding for the PHP Command LineInterface (28.9.2016, 13:46 UTC)

December 2016 - EU
Presented By

Steve Grunwell
December 15, 2016
20:00 CET

The post Building for the PHP Command Line
Interface
appeared first on Nomad PHP.

Link
PHP ClassesHow to Calculate VAT in PHP for any EU Country (28.9.2016, 06:36 UTC)
By Dave Smith
As you may know, VAT is a tax used in European Union countries. It is added to the price of products and services, but the actual percentage of the tax varies from country to country, as well from product to product or service to service.

If you or your clients need to add value-added taxes (VAT) within the European Union or validate VAT numbers, then there is a service available to get the latest rates for each country including rates within reduced rate categories.

You can also use the service to calculate VAT compliant prices for you, or reverse the calculation to get an amount without VAT if it has already been included.

Read this article to learn more about how you can use this service to help determine the VAT taxes to use on different purchases using PHP applications.
Link
SitePoint PHPSingle-File Symfony Apps? Yes, with MicroKernelTrait! (27.9.2016, 16:00 UTC)

A Single Page Application (SPA) offers a desktop experience to users of a web application by loading a single HTML page, and dynamically updating it as required without reloading. However, a Symfony application may have hundreds of classes, and in a basic application we end up with lots of files we don't really need.

Illustration of a programmer holding up an elephant

The latest versions of Symfony (2.8 and 3.0) introduce us to the concept of a Single File Application (SFA) - a super-slim application or micro-framework implemented in one file.

To follow along, you need to have a running web server and have your way of running web applications locally. See Laravel Valet article for a quick way of setting up a local development environment that doesn't require configuring a web server, virtual hosts and mucking about with a hosts file. Another option is our trusty Homestead Improved for a ready-to-go experience.

Step 1: Install Barebones Symfony

We are going to install Symfony with Composer as it allows us install only the main package. Create a folder where you usually have your web applications and let's call it sfa. I've got mine under ~/Sites/sfa. In it, we install Symfony:

composer require symfony/symfony

Now, create 2 folders inside sfa and name them app and web.

Step 2: The Front Controller

Inside sfa/web we will house our front controller - a file that receives all requests to the application, passes it to the right place for processing and returns the response to the client that made the request.

You can call this file anything, but you need to make sure your web server has been configured to find it in the correct place. Laravel has public/index.php, Drupal 8 has index.php, and Symfony has web/app_dev.php (during development) and web/app.php (during production). Since this is a Symfony application, let's call ours app_dev.php:

<?php

use Symfony\Component\HttpFoundation\Request;

require __DIR__.'/../vendor/autoload.php';
require __DIR__ . '/../app/SfaKernel.php';

$kernel = new SfaKernel('dev', true);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Two small differences between this file and a vanilla Symfony 3 installation.

Firstly, our kernel class is going to be in app/SfaKernel.php. Nothing stops us from calling it Kernel.php, but we want something different. Secondly, we opt not to call the loadClassCache() method. Our application is a slim one, without a good number of classes from a standard installation, so we can leave that method out for now.

Even though we're talking about a single file app, you'll notice it's not really a single file - we do have a front controller and a mini-kernel which does all the heavy lifting. That's in addition to all the other classes loaded from vendor. However, for all intents and purposes, starting and running a Symfony app from a single Kernel file can be regarded as a single file application.

Continue reading %Single-File Symfony Apps? Yes, with MicroKernelTrait!%

Link
Voices of the ElePHPantInterview with Jeff Geerling (27.9.2016, 10:00 UTC) Link
Stefan KoopmanschapWin a FREE PHPNW ticket (26.9.2016, 18:00 UTC)

I've said it before and I'll say it again: PHPNW conference, every first weekend of October in Manchester, is the best PHP conference I've ever been to. I buy my ticket blindly as soon as they go on sale every year. I've been accepted to speak very regularly in previous years and then raffle off my ticket. I was not expecting to need to do that this year, since I did not get accepted to speak. I was looking forward to experience PHPNW as a delegate this year.

Things have changed though. Unfortunately one of the speakers had to cancel their speaking and I've been asked to be the replacement. Of course I said YES! Speaking at PHPNW is a fantastic experience, and I'll gladly help them in a situation like this. But this also means that I don't need my ticket anymore.

This means, however, that I have a spare ticket. As it has become a tradition by now to raffle my conference ticket once accepted as a speaker, I'm again raffling my PHPNW ticket. The ticket has full access to all events of the main conference, including the hackathon on Friday, the social on Saturday and the Sunday conference day. The only thing is: The conference is THIS WEEKEND!

So: If you want to win my ticket, the only thing you need to do is send me an e-mail: stefan@ingewikkeld.net. Please only send an e-mail if you can actually make it this weekend. On Wednesday I'll do a random draw of the winner and send them an e-mail.

PHPNW will be awesome again. See you in Manchester!

Link
SitePoint PHPQuick Intro: PhpCompatibility for PHPCS – Are You PHP7 Ready? (26.9.2016, 16:43 UTC)

Sooner or later, there will come a time when you will need to migrate your projects to different PHP versions. How will you check if you're compatible with a PHP version different to the one you've been developing on?

One possibility is always to install the version of PHP we want to migrate to, run php -l or something like PHPSA to check for syntax errors, check the PHP documentation for known issues with the migration and hope for the best. Or, we can use some available third party tools to check for PHP version compatibility in our projects.

analyze vector image

Check compatibility with PHPCompatibility

PHPCompatibility is a set of sniffs we can install on top of PHPCS. This tool allows us to check our project's compatibility with both newer and older versions of PHP. If you are not familiar with PHP QA tools, PHPCS is a tool which inspects PHP, JavaScript, and CSS for different code violations based on different sets of coding standards.

The current iteration of PHPCompatibility supports PHP versions up to PHP 7.

Installing PHPCompatibility

PHPCompatibility can be installed via Pear or via Composer. For this particular case, we will install PHPCS using Composer and then deploy our PHPCompatibility coding standards directly on top of it.

For a local installation:

composer require "squizlabs/php_codesniffer=2.*"

After PHPCS is installed, let's move into the PHPCS /Standards folder that's located in /vendor/squizlabs/php_codesniffer/CodeSniffer/Standards and run:

git clone https://github.com/wimg/PHPCompatibility.git

This command will deploy the PHPCompatibility coding standard directly into our standards folder, together with the coding standards already bundled in PHPCS. To check if both PHPCS and PHPCompatibility were successfully installed just run the command:

./vendor/bin/phpcs -i

This will list all the installed standards. We should see PHPCompatibility among them.

For a global installation, the same method is valid, just be sure to use Composer's global require:

composer global require "squizlabs/php_codesniffer=2.*"

and then clone PHPCompatibility into the following folder:

~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards

Continue reading %Quick Intro: PhpCompatibility for PHPCS – Are You PHP7 Ready?%

Link
Michelangelo van DamPHP 7 on macOS Sierra (26.9.2016, 12:51 UTC)

PHP 7 on macOS Sierra

Apple has released the latest version of their OS X operating system to the broad public and many have already upgraded their mac devices. But as it goes with each release, Apple likes to do things a bit different making it quite challenging for PHP developers to stay current with the latest PHP version (or other versions).

This version of mac OS (11.12) comes pre-installed with PHP 5.6.24.

PHP 5.6.24 (cli) (built: Aug  8 2016 16:58:37)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies


Good for Apple, but this version reaches end-of-life support by the end of this year, so it would be great if you could upgrade to PHP 7.0 or even play with the latest PHP 7.1 release candidates.

Of course you can always turn to Homebrew, XAMMP or whatever package manager you use for easy installations, but taking the compiling path is just as complicated. The failure is equal in most situations.

Configuring PHP on macOS Sierra triggers the following error:


apxs:Error: /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/apr-1-config not found!.
configure: error: Aborting


In this version of Apple's OS the Apache Portable Runtime was not included. You need to download it and compile it yourself.

wget http://apache.proserve.nl//apr/apr-1.5.2.tar.bz2
wget http://apache.hippo.nl//apr/apr-util-1.5.4.tar.bz2


Check the checksums!

tar -xjf apr-1.5.2.tar.bz2
tar -xjf apr-util-1.5.4.tar.bz2

cd apr-1.5.2/
./configure
make
make install

cd ../apr-util-1.5.4/
./configure --with-apr=/usr/local/apr
make
make install

sudo mkdir -p /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/
sudo ln -s /usr/local/apr/bin/apu-1-config /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/apu-1-config
sudo ln -s /usr/local/apr/bin/apr-1-config /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/apr-1-config


Now you can hapily compile and run the latest PHP version on the latest OS from Apple. See my previous article Compile PHP 7 on Mac OS X 10.11 "El Capitain" for instructions.


Happy PHP-ing!

Link
SitePoint PHPMail Logging in Laravel 5.3: Extending the Mail Driver (23.9.2016, 16:00 UTC)

Laravel Logo

One of the many goodies Laravel offers is mailing. You can easily configure and send emails through multiple popular services, and it even includes a logging helper for development.

Mail::send('emails.welcome', ['user' => $user], function ($m) use ($user) {
    $m->to($user->email, $user->name)->subject('Welcome to the website');
});

This will send an email to a new registered user on the website using the emails.welcome view. It got even simpler with Laravel 5.3 using mailables (but the old syntax is still valid). Here's an example:

# Generate a new mailable class
php artisan make:mail WelcomeMail

// app/Mail/WelcomeMail.php

class WelcomeUser extends Mailable
{

    use Queueable, SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->view('emails.welcome');
    }
}

// routes/web.php

Route::get('/', function () {
    $user = User::find(2);

    \Mail::to($user->email)->send(new WelcomeUser($user));

    return "done";
});

Laravel also provides a good starting point for sending mails during the development phase using the log driver, and in production using smtp, sparkpost, mailgun, etc. This seems fine in most cases, but it can't cover all the available services! In this tutorial, we're going to learn how to extend the existing mail driver system to add our own.

To keep our example simple and straightforward, we're going to log our emails to a DB table.

Continue reading %Mail Logging in Laravel 5.3: Extending the Mail Driver%

Link
LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP