Laravel & WordPress Integration
Routing and Controller: In general, a back-end web application has one primary responsibility: to read each request and create a response depending on the content of the request. The controller is a class responsible for transforming the request to the response by calling application methods, while the router is a mechanism that helps you to detect which controller class and method you should execute for a particular request.
In the real-life applications, you shouldn’t call ORM from the controller. Instead, you should call a service that will try to find the post or throw HTTP 404 Exception.
Applications have a lot of different services and components, with various interdependencies. You need to store all the information about the created objects and their dependencies somehow.
Here comes our next component: Service Container. It is a PHP object that creates requested services and stores information about the created objects and their dependencies.
Also, facades provide a “static” interface to classes that are available in the application’s service container.
Great comparison article by comparing modules both in Symfony and Laravel.
There are several uses for a repository pattern. It can be used to avoid having to use the singleton pattern for classes that you need to reuse the same instance of in most, but not all situations throughout your project. For example, you may have a settings class that holds the settings for your plugin. You’d probably want to use the same instance of that class most of the time but have flexibility.
Integrating WordPress inside Laravel
- corcel/corcel is used to create a Laravel or any PHP application, but use data directly from a WordPress database.
There is an example on how to create separate Laravel app that will transfer Google Docs to Wordpress using WP’s JSON Rest API.
Here is the most interesting part of the article, Laravel, and WordPress hybrid. If you are Laravel fan, can’t imagine yourself coding in other frameworks, and have complex applications with a lot of content, Corcel is what u need for happiness. Use WordPress as admin panel and data manipulation, but code controllers, views with Laravel.
Integrating Laravel into WordPress
I want this so I can write flexible plugins with proper coding architecture from the start. There are lot of projects that help in creating plugins using Laravel inspired frameworks, but lot of those are not maintained anymore. Here are some of them that are active:
wordplate/wordplate if boilerplate built with Laravel and Symfony components.
Based a lot on Bedrock and Roots products with a familiar setup for every Laravel enthusiast. The thing that stand up is using
Acorn | roots/acorn could be my solution.
Provides a way to load a Laravel application container inside of WordPress, while respecting the WordPress lifecycle and template hierarchy. Quite poorly documented for now, but it is THE ONLY actively maintained that exactly fits my requirements and is used in their popular Sage theme
wpbones/WPBones has plugin bolerplate wpbones/WPKirk is active and is good fit.
It’s not exactly Laravel, as it is using wpbones/WPBones framework, but very similar. wpbones/WPBones framework is actively maintained and has nice documentation at wpbones/WPBones Wiki. Semms that it really fits my use case: Typical Workflow
WP Emerge has a starter plugin htmlburger/wpemerge-plugin.
It is actively developed but not sure how stable it is. Coded by amazing author Atanas Angelov that now works exactly at Automattic. Uses htmlburger/wpemerge micro-framework inspired by Laravel and such. There is also a Starter Theme
Plugin boilerplate bayareawebpro/wp-lumen-plugin-framework that integrates a full Lumen Micro-framework instance within the WordPress Plugin. It is exactly what I need, but it is using Laravel Lumen aka leaner version of Laravel. Development also ended.
LaraPress yousuf-hossain-shanto/larapress is plugin boilerplate in Laravel. Also dead.
Composer Based Whole Setup WordPress Boilerplates
There are a couple of Composer based WP boilerplates and they all feature improved folder structure among other great features.
The undisputed king that can be used with Acorn Laravel implementation from same company
Personal WordPress boilerplate based-on Bedrock by Roots
After careful research, I’ve decided on Bedrock as it is updated regularly and it is also the most famous with most articles around the net. It also supports enabling classic plugin upload and there are quite a good solutions to solve problems like that; paid plugins and such.
Examples of using Laravel in a plugin
- withfatpanda/illuminate-wordpress is retired project, but good introduction withfatpanda/bamboo is plugin boilerplate Author: You can’t use Laravel Facades in Illuminated WordPress Plugins
Handy Composer Tools to Use
franzliedke/studio allows using the packages you’re working on NOT from the online Packagist repository but symlink them from the local filesystem. It is made to ease a process of developing Composer packages itself, but can be used in various ways. It is like orchestral/testbench but not Laravel-only.
SanderSander/composer-link also adds ability to link local packages for development with composer
Best explanation on The Laravel Service Container demystified - Beyond Code
On composer race conditions in Wordpress
Problem explained: A Narrative of Using Composer in a WordPress Plugin – WP Tavern
humbug/php-scoper: 🔨 Prefixes all PHP namespaces in a file/directory to isolate the code bundled in PHARs. coenjacobs/mozart: is older version of php-scoper - Developers tool for WordPress plugins: Wraps all your projects dependencies in your own namespace, in order to prevent conflicts with other plugins loading the same dependencies in different versions.