Wednesday, September 28, 2016

9 Hot Tips to Enhance Your Spark Experience

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:

<select class="form-control" v-model="registerForm.country"↩
    lazy>
    @foreach (app(Laravel\Spark\Repositories\↩
        Geography\CountryRepository::class)->all()↩
        as $key => $country)
        <option value=""></option>
    @endforeach
</select>

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%


by Christopher Pitt via SitePoint

No comments:

Post a Comment