Laravel integrates the WordPress extension package-Corcel

Source: Internet
Author: User
Tags wordpress database

Corcel is an integrated Wordpress extension package that can be applied to the Laravel framework. It publishes articles in the background of wordpress and uses its interface to conveniently retrieve articles in Laravel, installation and use are also very simple.

Corcel Github: https://github.com/jgrossi/corcel

The general process is described here. For detailed installation and usage, see the instructions on the github homepage.


I. Database migration

You need to migrate the Wordpress database to the Laravel server, that is, to create a new database and store the wordpress data table.

Add the wordpress database definition to the connections array in the config/database. php configuration file of laravel.

Download the SQL file through wget on Alibaba Cloud CentOS and use source to import the SQL file to the database. For details, see wget and source to import the SQL file.

II. Create a model class

To use the Corcel extension package function, you need to use the model class, which inherits the Corcel class, for example, creating a Wp. php model in the Models folder:


<? Php
 
Namespace App \ Models;
 
Use Corcel \ Post as Corcel;
 
Class Wp extends Corcel
{
Protected $ connection = 'wordpress ';
}

The connection attribute defines the database to be connected and inherits the Corcel \ Post class to implement various interfaces provided by Corcel.

III. Test Laravel's Wordpress retrieval

Create a new TestController controller and write a test method in the controller:

PHP


// Laravel query wordpress articles
Public function testWp ()
{
$ Post = Wp: find (9569 );
Echo $ post-> post_title;
}
Add routing rules:

PHP

1
Route: get ('/test/wp', ['uses '=> 'testcontroller @ testWp']);
Open it in the browser and you will see the effect. Read the articles in the wordpress database and output the article title.

For more information, see the Github homepage of the Corcel extension package.

Supplement:

Override the getUrlAttribute method:

To make it easier to get the real url of a blog post, instead of like http://www.111cn.net /? P = 9562, the actual url is http://www.111cn.net/2016/03/laravel-wordpress-corcel, you only need to re-write the extension package vendor/jgrossi/corcel/src/Post in the new Model. php's getUrlAttribute method is enough, Like this:


<? Php
 
Namespace App \ Models;
 
Use Corcel \ Post as Corcel;
 
Class Wp extends Corcel
{
Protected $ connection = 'wordpress ';
Protected $ domain = 'http: // blog. tanteng. me /';
 
// Rewrite the method for obtaining the wordpress article url
Public function getUrlAttribute ()
    {
Return $ this-> domain. date ('Y/m/', strtotime ($ this-> post_date). $ this-> slug .'/';
    }
}
Print in controller:


// Laravel query wordpress articles
Public function testWp ()
{
$ Post = Wp: find (9569 );
// Dd ($ post );
Echo $ post-> post_title;
Echo '<br> ';
Echo $ post-> url;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.