PHP Classes
elePHPant
Icontem

How to use Corcel Laravel WordPress API to create Web Applications - Corcel (Laravel + WordPress) package blog

Recommend this page to a friend!
  All package blogs All package blogs   Corcel (Laravel + WordPress) Corcel (Laravel + WordPress)   Blog Corcel (Laravel + WordPress) package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to use Corcel Lar...  
  Post a comment Post a comment   See comments See comments (1)   Trackbacks (0)  

Author: Junior Grossi

Posted on:

Package: Corcel (Laravel + WordPress)

WordPress is certainly the most popular PHP application ever. However, many PHP developers do not like to develop on top of its code because it is in great part not based on object oriented code and does not follow the latest PHP framework trends.

Nowadays many developers love newer frameworks like Laravel to build their applications.

The Corcel framework brings the two worlds: WordPress and Laravel or other modern frameworks.

Read this article to learn how you can use the Corcel framework to build applications on the top of WordPress backend.




Contents

Introduction

What is Corcel?

What you need to use Corcel?

So can I use MVC with WordPress?

Post Type and Custom Fields

Conclusion

WordPress + Laravel

Introduction

Developing Web applications has to be fun, and fast. Of course each application has its own requirements and its life cycle.

WordPress is a powerful CMS written in PHP that allows you to create sites that can be produced at a very fast pace. On the other hand, it does not followed the recent PHP evolution and the latest conventions, but you can balance this, using it with your framework of choice, including Laravel.

What is Corcel?

I think the Wordpress administration panel is amazing. It has a bunch of plugins that allow you to include fields, post types, images, crops and more, very quickly. The best of this is that it works wonderfully!

That is why Corcel was developed. It allows you to get data from WordPress database with ease.

What you need to use Corcel?

Just create your PHP application skeleton, using Laravel or another framework, a WordPress installation and the Corcel package installed with Composer.

So can I use MVC with WordPress?

Yes! You can have your application controllers, models, views, and continuing to work with WordPress.

Corcel brings you a model collection to retrieve Posts, Pages, Menus, etc.. You can even use different databases connections, one for your Laravel application and another just for WordPress.

Here is sample configuration for multiple database connections using Corcel:

'connections' => [

    'mysql' => [ // this is your Laravel database connection
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'app',
        'username'  => 'admin'
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],

    'wordpress' => [ // this is your Corcel database connection, where WordPress tables are
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'corcel',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],

],

And here follows how to start getting what you need from the WordPress database:

<?php
// ...
public function index()
{
    $posts = Post::published()->take(10)->get();
    $page = Page::where('post_name', 'about')->first();
    return view('posts.index', compact('posts', 'page'));
}
// ...

Post Type and Custom Fields

Do you use Advanced Custom Fields (ACF)? You can get all the custom fields too:

<?php
 // ...
 $post = Post::find(1);
 $avatar = $post->meta->avatar;
 $phone = $post->meta->phone;

You can create custom models related to custom post types:

<?php
// ...
use Corcel\Post as Corcel;

class Service extends Corcel
{
    protected $postType = 'service';
}

Conclusion

There are many more features you can check directly in the package README file.

You can use Corcel with any PHP framework, even micro frameworks like Slim, Silex, etc.. It allows you to get all WordPress administration panel data and gives you the opportunity to organize your project using custom routes, controllers, models and views.

Give Corcel a try, suggest new features and contribute. You are welcome!


You need to be a registered user or login to post a comment

1,392,680 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

1. Nice One - Lucky Chauhan (2016-06-07 11:21)
I really liked it.... - 0 replies
Read the whole comment and replies




  Post a comment Post a comment   See comments See comments (1)   Trackbacks (0)  
  All package blogs All package blogs   Corcel (Laravel + WordPress) Corcel (Laravel + WordPress)   Blog Corcel (Laravel + WordPress) package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to use Corcel Lar...