Building a Laravel Translation Package – Scaffolding

Published on by

Building a Laravel Translation Package – Scaffolding image

In Part 1, we introduced that this series would cover the process of building and maintaining an open-source package for Laravel. Check it out for an overview of what we’ll create in this series. Next, we are going to get to work on scaffolding a new Laravel package.

Scaffolding the Package

The first thing you need to do when creating a PHP package of any kind is setting up the repository, and most importantly the composer file. There’s no de-facto standard approach to organizing Composer packages; however, I will walk you through my approach.

I tend to start by installing a fresh version on Laravel using the installer. Using a test Laravel installation gives me a shiny new playground in which to work.

In the root of my newly installed Laravel project, I create a directory called packages. Within this directory, I create a directory structure for my package which aligns with the GitHub repository where the code will ultimately reside – in this case, joedixon/laravel-translation.

From within the package directory, I run git init to set up version control. Next, I run composer init and follow the instructions which create my composer.json file for managing any dependencies I may require and for later submission to Packagist.

Next, I create an src directory at the root of my package which will contain all of the domain specific logic of the package.

In the composer.json file, I add the following PSR-4 configuration to tell Composer how to autoload my package namespace:

"autoload": {
"psr-4": {
"JoeDixon\\Translation\\": "src"
}
},

Finally, before I write a single line of code, I amend the composer.json file at the root of the Laravel application to tell it how to load my app.

"require": {
"joedixon/laravel-translation": "dev-master"
},
"repositories": [
{
"type": "path",
"url": "./packages/joedixon/laravel-translation",
"options": {
"symlink": true
}
}
]

Hopefully, the require section will look familiar to you. The repositories section, however, may not.

The repositories section tells Composer to symlink the package from the local installation. Using this approach allows us to work on and test the package locally without having to composer update anytime something changes.

When using the local file path approach, it’s worth noting the package can be anywhere on your machine; however, I like to keep it contained within the application structure for development.

Now that the package has been bootstrapped and autoload configured, we can start writing some code.

Package structure

When building a package, I try to mirror the Laravel application structure as much as possible. As such, everything typically found in my app directory such as controllers, console commands, event listeners, etc. go into my package src directory and things such as routes and resources live at the root of the package.

Service Provider

For Laravel to start using the package, we need to build a service provider. The service provider class is where we will bootstrap the package by performing actions such as binding services to the container, registering routes, publishing configuration, and just about anything else you can imagine that need tying into the Laravel app!

Tip Typically, I will run php artisan make:provider TranslationServiceProvider which will scaffold a service provider in the Providers directory of the Laravel application. I then move it to my package and update the namespace accordingly.

At this stage, I will register things like routes, config, views, and translations without necessarily fleshing out all of the details. I find doing it in this way makes things faster later on in development.

Testing

No good package would be complete without tests.

Setting up tests in a package can be quite tricky; mainly if you want to be able to access Laravel’s testing helpers. Fortunately, the orchestra/testbench package makes it possible to use all of Laravel’s native test helpers within your packages tests.

Run composer require --dev orchestra/testbench to install it as a development dependency. Running Composer should give you the following configuration in the composer.json file:

"require-dev": {
"orchestra/testbench": "~3.0"
}

Before we’re done, let’s commit what we have to the master branch:

# within ./packages/joedixon/laravel-translation
echo "vendor/" >> .gitignore
git add .
git commit -m"Initial Commit"

That just about wraps up the package scaffolding. Next time, we’ll start building out some package functionality, starting by building out the file based translation driver.

Joe Dixon photo

Founder and CTO of ubisend. Proud Father to two tiny heroes, Husband, developer, occasional globetrotter.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Add Comments to your Laravel Application with the Commenter Package image

Add Comments to your Laravel Application with the Commenter Package

Read article
Laravel Advanced String Package image

Laravel Advanced String Package

Read article
Take the Annual State of Laravel 2024 Survey image

Take the Annual State of Laravel 2024 Survey

Read article
Upload Files Using Filepond in Livewire Components image

Upload Files Using Filepond in Livewire Components

Read article
The Best Laravel Tutorials and Resources for Developers image

The Best Laravel Tutorials and Resources for Developers

Read article
Introducing Built with Laravel image

Introducing Built with Laravel

Read article