Spark is a free and open-source web application framework. It uses a domain specific language authored in Java. The intention of Spark is to create web applications in Java with little effort. There are several ways you can enhance your Java experience.
1. Don’t keep all the base files
You may be wondering whether it is okay to remove the base files from the normal Spark installation. Most people are afraid that removing the auth controllers (in app/HTTP/controllers/auth) might break the login and registration system.
Interestingly, you will discover that Spark doesn’t utilize these files. In any case, if you add routes to the files, you will just have more problems. These auth controllers happen to share one session driver (auth guard), meaning that if you log in through one, the other one will authenticate you.
If you attempt to register using the non-Spark controllers, you will be missing crucial Spark information in your user and team accounts. The wisest thing would be just to delete the auxiliary auth controllers.
2. Utilize simple repositories
Repositories are more like static config lists that you can loaded into the IoC container. They are basically 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',
];
}
}
Some instances are used in 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="{{ $key }}">{{ $country }}</option>
@endforeach
</select>
These repositories come highly recommended for country and state data. Use the following style for your own data:
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)
);
}
}
3. Refrain from using caret (^) Laravel dependencies
Laravel evolves very fast, which keeps developers’ interest piqued, but has maintenance disadvantages. Replace any ^x.x versions with ~x.x.x version numbers. Spark 1.x originates from Laravel 5.2.x, and if you use ^5.2, a call to composer upgrade will change Laravel to 5.3 which will introduce breaking changes.
Do not assume that minor version bumps will be safe, as Laravel does not follow Semver. In the upgrade of minor versions, Taylor is reasonable in difficulty involved. If the breaking changes take too long to tone down, they will be warded off until the next version.
Spark.2.x is built from Laravel 5.3.x, which is a good reason for you to begin there if possible. This means you will have all the best new features in 5.3.x. It is much harder to upgrade an existing application than it is to start a new one.
4. Use the Docs
Spark depends on Stripe, Cashier or Braintree. Don’t feel threatened; you don’t have to be a whiz in these things. Before you touch on any of them though, be sure to read up on the documentation that relates to them.
5. You don’t have to learn VueJS
VueJS is a JavaScript framework that builds interfaces. Its very design is meant to be adaptable, so it easily integrates into other JavaScript users. It is very popular and simplifies web development. It can be used in existing projects and pages to add interactivity.
You are not under any pressure to learn VueJS before using Spark. The docs will fill in any gaps, and the bundled views will be of even more help by giving plenty of examples. If you so wish, you can make your app specific code to be HTML or PHP.
6. Use Forge as a host
Forge is a reliable, sensible and affordable server manager that leans towards Laravel applications. You can also host other non-Spark things, and it will make your Spark experience better.
Forge offers:
-
Several cloud providers: choose between several, or you can create your own custom VPS
-
SSL Certificate Management
-
Site and sub domain management: Configures domains, sub domains and wild card domains with a control panel
-
Manages queue workers: Configures such that the workers run continuously
-
Makes cron jobs simple: Configures such that commands run continuously or on the schedule you want.
-
Balances loads: Interface that will distribute your traffic to other servers
-
Provides horizontal scaling: provides an interface for private network configuration
- Provides up to date server configurations including Ubuntu 16.04 LTS
7. Re-arrange middleware
Route model binding is one of the best features of Laravel. Routes are defined as:
use App\ Http\ Controllers\ CustomersController;
Route::get("customers/:customer", [
"uses" => CustomersController::class,
"as" => "customers.edit",
]);
Actions are then defined thus:
/**
* @param Customer $customer
*
* @return Response
*/
public
function edit(Customer $customer)
{
return view("customers.edit", [
"customer" => $customer,
"groups" => Groups::all(),
]);
}
When this code is run as controller action, automatically, $customer is fetched from the database.
8. Create your helpers
It’s nothing high tech or complex, and it’s not too much code to type in. For example:
use App\ User;
use App\ Team;
if (!function_exists("user")) {
/**
* @return App\User
*/
function user() {
return auth() - > user();
}
}
if (!function_exists("team")) {
/**
* @return App\Team
*/
function team() {
return user() - > team();
}
}
9. Test webhooks with Ngrok
Building a product around more than one third-party service is not uncommon. When you need to post to Twitter or send an SMS, you need a web hook URL. These are application routes that gather information from the third party services. It could either be confirmation information from the shipping provider, or a report on delivery from the transaction mail service.
You could upload the application to a public server. This means that the web hook URLs can be reached by the third party service that will, however, slow your development cycles. The other alternative is to give your local server an unknown URL by using ngrok. Install ngrok with Homebrew:
brew install ngrok
Serve the Laravel application with the PHP server:
php artisan serve
In a different tab, you can now run ngrok:
ngrok HTTP 8000
10. Eliminate Caching
The advantage of Spark over other frameworks is the fact that intermediate results can be stored in the cluster memory. Caching is both a good and a bad thing. This is because you always have the option of caching something you most probably use a lot of times. The impact on your memory is not equal to the task of actually recomputing it. This means it is just easier to recompute the windows you do not need, rather than storing them in the cluster memory. Taking the pressure off the memory makes a huge difference; up to 1.75x speeding of the code. This elimination of caching needs a lot of application insight.
11. Materializing Sample Windows
It makes no sense and is wasteful, to say the least, to store and to keep shuffling huge sample windows. They may be cheap to compute, but they are not needed at all. It is of more use to record effort structures than to keep a bunch of sample windows. The effort structures have wattage, the name of the activity and the beginning and end timestamps.
To do this from the ActivitySliding trait, you can generalize the sliding window behavior. It lets clients be able to pinpoint their changes for different windows individually. When Sparks does a job, it lines up many tasks into stages without coordination or data shuffle.
The stages are made up of different highly utilizing tasks. This means the workers ought to concentrate more on computing than driver coordination.
We would like you to enrich these tips with your Spark experience. Share with us in the comments section below.