PHPackages                             larawelp/theme - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. larawelp/theme

ActiveLibrary[Framework](/categories/framework)

larawelp/theme
==============

The LaraWelP theme for WordPress, powered by Laravel.

v0.0.10(2y ago)28MITPHPPHP ^8.0

Since Aug 20Pushed 2y ago1 watchersCompare

[ Source](https://github.com/larawelp/theme)[ Packagist](https://packagist.org/packages/larawelp/theme)[ Docs](https://github.com/larawelp/theme)[ RSS](/packages/larawelp-theme/feed)WikiDiscussions 0.x Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (11)Used By (0)

Laravel in WordPress Theme
==========================

[](#laravel-in-wordpress-theme)

Laravel is a web application framework with expressive, elegant syntax. It's one of the most popular PHP frameworks today.

LaraWelP brings the Laravel Framework into WordPress, which allow us to have all the benefits of Laravel. So you can create themes with less effort, more enjoyment!

Requirement
-----------

[](#requirement)

The 99% of LaraWelP is just the regular full stack PHP Framework [Laravel](https://laravel.com/). So if you have never heard of it, you're going to want to take a look at it before you can go any further.

For those who are already familiar with Laravel, it should be a piece of cake for you to get started with LaraWelP.

What LaraWelP is and is not
---------------------------

[](#what-larawelp-is-and-is-not)

**LaraWelP is not a framework for general purpose WordPress theme development.**

Yes, it is a framework but not for general WordPress theme development. LaraWelP is aimed at helping create "homemade theme" rather than general purpose theme. So if you want to create themes with a bunch of theme options for sales or just for free distribution, you probably want to take a look at the following frameworks instead.

- [Piklist](https://piklist.com/product/piklist/)
- [Gantry](http://gantry.org/)
- [Unyson](http://unyson.io/)

What's the diffrence between the original Laravel?
--------------------------------------------------

[](#whats-the-diffrence-between-the-original-laravel)

I'd say almost no differences there, except some additional tweaking, which gets Laravel to work well inside a WordPress theme. So basically you could do anything that you could do with Laravel, it's just the regular Laravel inside a WordPress theme. If you are curious about what exactly have been modified, taking a diff to the original Laravel would make sense for you.

Known issues
------------

[](#known-issues)

WordPress uses `mysqli` for connecting to the database, but Laravel uses `PDO`. So if you want to use the `DB` facade of Laravel, you have to install the `pdo_mysql` extension for PHP. A `mysqli` driver for Laravel is in the works for LaraWelP, but for now, you might get errors in some environments where PDO is not available (eg: wp-env).

Get Started
===========

[](#get-started)

Installation
------------

[](#installation)

You can install LaraWelP by issuing the following command via [Composer](https://getcomposer.org/). `composer create-project --prefer-dist larawelp/theme `

Note that **the MySQL server and the web server must be running before you can issue the `composer create-project` command** to install LaraWelP. Because after Composer finishes the installation, it's going to run an artisan command, which requires MySQL server and the web server that host the WordPress be running at the time you issuing the command.

Also, notice that if you are on Mac and use MAMP or similar application to create your local server environment you may need to change your `$PATH` environment variable to make Composer use the PHP binary that MAMP provides rather than the OS's built-in PHP binary.

Routing
-------

[](#routing)

LaraWelP replaced the original `UriValidator`(`Illuminate\Routing\Matching\UriValidator`) with its own one to allow you to specify WordPress specific routes, like "archive" or "page" or "custom post type" ex.

To define a WordPress specific route, just by providing a "page type" as the first argument.

For example:

```
// The "about" page
Route::any('page.about', Controller@method);

// The child page "works" of "about".
Route::any('page.about.works', Controller@method);

// Any child page of "about".
Route::any('page.about.*', Controller@method);

// Any descendant page of "about".
Route::any('page.about.**', Controller@method);

// Grouping multiple routes that sharing a common `prefix`.
Route::group(['prefix' => 'page'], function () {

    Route::any('about.contact', function () {
        return 'Foo'; // equivalent to
    });

    Route::any('service.*.price', function () {
        return 'Bar'; // equivalent to
    });

});

// IMPORTANT !
//
// Routes that has a higher specificity should be
// placed more above(earlier) than the routes that have a lower specificity.
// Why? If you place the routes that have a lower specificity,
// the subsequent routes that have a higher specificity will be ignored.
//
// The following routes have a lower specificity than the above ones.
// So you want to place them here.

// Generic pages
Route::any('page', Controller@method);

// Front page
Route::any('front_page', Controller@method);

// Post archive index page
Route::any('archive', Controller@method);
```

Here's some notes you should keep in mind.

- You can use a "dot notation" to specify the hierarchy for pages and taxonomies.
- You can use the wild card to specify any child/descendant page/term of a parent/ancestor page/term.
- You should care about the order of your routes. Routes that has a higher specificity should be placed more above than the routes that have a lower specificity.

What's more, you can even write your own routes by URI, and it just works.

```
// This will use the original UriValidator of Laravel.
Route::get('/my/endpoint', function () {
    return 'Magic!';
});
```

Models
------

[](#models)

LaraWelP comes with some general purpose models like `Post` or `Term` model. Note that they are not an implementation of ORM like the Laravel's Eloquent Model. They are just a simple wrapper for WordPress's APIs that encapsulate some common logic to help you simplify your business logic.

You can find those models in `LaraWelP\WpSupport\Model`. Because the `Post` model is the most frequently used model, for convenience, a `Post` Class that extends the `LaraWelP\WpSupport\Model\Post` has brought to your `app/Models` directory already.

Let's take a look at an example.

See you have a route like this :

```
Route::any('archive', 'Generic\Archive@index');
```

In your controller `app\Http\Controllers\Generic\Archive` :

```

```

where `$post` should be a `Post` model object.

Usually you don't want to use the `@loop` directive. Because it'll introduce some unnecessary overheads. Keep in mind that always prefer `@foreach` to `@loop`. Except you want to access some properties like `content` or `excerpt` which requiring must be retrieved within "The Loop", otherwise never use the `@loop` actively.

Theme Options
-------------

[](#theme-options)

Setup the custom post type, register the navigation menus ... There always are some common tasks you have to deal with when you start to build a WordPress theme. The `app/config/theme.php` is where you define all your common tasks.

Some basic options are predefined for you. Take a look at the [config/theme.php](https://github.com/larawelp/theme/blob/master/config/theme.php).

Also, you can create your own options by adding new static methods to the `App\Providers\ThemeOptionsProvider`. The name of the method will become to an option.

Actions and Filters
-------------------

[](#actions-and-filters)

You define your actions and filters in `App\Providers\EventServiceProvider` just like the laravel's event.

The following example adding a `pre_get_posts` action, and the `handle` method of `App\Listeners\MainQueryListener` will be called for this action.

```
