Tinkerwell - The PHP Scratchpad

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.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

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

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

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

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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

The latest

View all →
Speeding Up Laravel News With Cloudflare image

Speeding Up Laravel News With Cloudflare

Read article
Livewire 4 Support in Laravel VS Code Extension v1.4.3 image

Livewire 4 Support in Laravel VS Code Extension v1.4.3

Read article
Fair Queue Distribution with Laravel Balanced Queue image

Fair Queue Distribution with Laravel Balanced Queue

Read article
Migrating Laravel News from Laravel Forge to Cloud image

Migrating Laravel News from Laravel Forge to Cloud

Read article
Laravel News Is the Live Stream Partner for Laracon EU 2026 image

Laravel News Is the Live Stream Partner for Laracon EU 2026

Read article
Query Builder Expression Aliases in Laravel 12.48 image

Query Builder Expression Aliases in Laravel 12.48

Read article