Home  >  Article  >  CMS Tutorial  >  How to use Laravel with WordPress

How to use Laravel with WordPress

藏色散人
藏色散人forward
2022-01-07 15:23:544364browse

The following column WordPress Tutorial will introduce to you how to use Laravel in WordPress. I hope it will be helpful to friends in need!

Corcel lets you use Laravel in WordPress

Have you ever thought that you can use Laravel or any PHP framework in WordPress? Corcel can help you achieve it!

Developing website applications should be fast and fun. Of course, each application will have its own needs and life cycle.

WordPress is a powerful CMS written in PHP that you can use to create your product very quickly. However, it does not follow recent PHP changes and conventions, but you can balance this out by using it with other frameworks like Laravel.

Corcel

I think the WordPress admin panel is great. It has a bunch of plugins that allow you to quickly generate fields, post types, images, crops, and more. This is really great!

That’s why Corcel makes it easy for you to get data from your WordPress database. You just need to use Composer to install WordPress and Corcel in your PHP application framework (Laravel or other framework).

Of course we can also use MVC in WordPress!

You can build controllers, models, and views for your WordPress. Corcel creates a collection of models for you to retrieve posts, pages, menus, etc., and can even connect to different databases, one for Laravel and another for WordPress.

<?php // File: /config/database.php
&#39;connections&#39; => [
    'mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'app',
        'username'  => 'admin'
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],
    'wordpress' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'corcel',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],
    
],

Let’s start getting what you need from the WordPress database:

<?php // File: /app/Http/Controllers/AnyController.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 field

I don’t know if you use Have you tried the Advanced Custom Fields Feature (ACF)? Here you can also get all the custom fields:

<?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';
}

For more features, you can directly View it in the GitHub (https://github.com/corcel/corcel) warehouse.
You can use Corcel with any PHP framework, even micro-frameworks like Slim, Silex. It gives you access to all WordPress admin panel data and lets you organize your project with custom routes, controllers, models, and views.

Come and give Corcel a chance. You are also welcome to give some suggestions or directly contribute code, thank you!

The above is the detailed content of How to use Laravel with WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete