Combining WordPress with MVC

The concept of cooperation of WordPress with Laravel (or any other MVC framework) is based on the following assumptions:

  • we use WordPress as the backend, we write here entries, pages etc., which is treated as a place for storing content
  • Laravel as a frontend means processing of stored data, displaying them on the page

An example of how to make a simple application in Laravel is available here. Description uses LAMP environment (Linux, Apache, MySQL, PHP).

Stage 1

Copy the folder with fresh WordPress into Laravel directory: app/wordpress.

Stage 2

Create virtual hosts, one for Laravel and one for the WordPress administration panel. Example:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
        ServerName wp-lvl.loc
        ServerAlias www.wp-lvl.loc

	DocumentRoot /var/www/FOLDER-TestApps/wp-lvl/public
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
	

	ServerAdmin webmaster@localhost
        ServerName wp-admin.wp-lvl.loc
        ServerAlias www.wp-admin.wp-lvl.loc

	DocumentRoot /var/www/FOLDER-TestApps/wp-lvl/app/wordpress
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Where the “wp-lvl” is a directory with the Laravel. If you are using a local server on your home computer (as here in this case), you need to add another entry of new virtual hosts in the /etc/hosts file. Restart the server.

After that the access to the backend of WordPress will be as wp-admin.domain.com. And front-end – domain.com. However, do not go to the specified addresses yet.

Become a Professionalist in Web Application Development! Enroll In The Course Of Laravel You will also learn how to create hybrid applications for smartphones

Stage 3

Edit wordpress/index.php file. Write at the top:

header("Location: ./wp-admin");
exit();

It will redirect us to the WordPress administration panel, because only for this purpose we want to use it.

Stage 4

Go to the address wp-admin.domain.com and install WordPress as you usually do.

Stage 5

Set the access data to the database for Laravel – the same as for WordPress.

Stage 6

Edit public/index.php file. Write at the top:

define('WP_USE_THEMES', false);
/** Loads the WordPress Environment and Template */
require __DIR__.'/../app/wordpress/wp-blog-header.php';

From now all WordPress functions and classes are available in the Laravel application.

Stage 7

Edit app/routes.php file. Write at the end:

Route::get('/posts', function()
{
global $wpdb;
$query = new WP_Query(array(
'post_type' => ['page','post']

));
$posts = $query->get_posts();

return View::make('posts')->with('posts',$posts);
});

So when you type “/posts” in the address bar it will load “posts” view with the available data there about entries that we pull out by WordPress methods.

Stage 8

Create mentioned view in app/views/posts.blade.php. Save there:

<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Laravel with WordPress</title>
</head>
<body>
<div class="welcome">
{{ dd($posts) }}
</div>
</body>
</html>

Now, after loading the /posts address in our Laravel application we see displayed $posts object with data ready for processing in this view, this data have been created in the WordPress admin panel.

 

 

enroll in the course of Laravel framework >>>> or affiliate program
Share

Leave a Reply

Your email address will not be published. Required fields are marked *