前回は、WordPress側の設定をしました。
今回は、FuelPHP側の設定をしていきましょう!
目次
ルーティングを設定
fuel/app/config/routes.php
のルーティング設定を変更します。
<?php
return array(
'_root_' => 'pages/index', // The default route
'_404_' => 'pages/404', // The main 404 route
// Fuel側でページを作っているものを先に書いていきます
'about' => 'pages/about',
// MW WP Formとかのプラグインを使ったさいのルーティング
'inquiry' => 'inquiry/index',
'inquiry/confirm' => 'inquiry/confirm',
'inquiry/complete' => 'inquiry/complete',
'inquiry/error' => 'inquiry/error',
// ここより上にページのルーティングを記載すること
// 下記のものはWordPressのルーティングになります。
'blog' => 'posts/index', // 一覧
'blog/(category|tag)/(:name)?' => 'posts/index', // カテゴリーやタグ一覧
'blog/date/(\d{4})(/\d{2})?' => 'posts/index', // アーカイブ
'blog/page/(\d{1,})?' => 'posts/index', // ページング一覧
'blog(/:id)?' => 'posts/detail/$1', // 詳細ページ
// WordPressの固定ページのルーティング
'(:name)?' => 'page/index/$1',
);
Controllerを作成
テンプレートのControllerを作成してもいいですし、そのままControllerを作成してもいいです。
ここでは、テンプレートコントローラーを用いた方法で作成していきます。
base.php
これの設定はお好みで作成してください。
<?php
/**
* Template Base
*/
class Controller_Base extends Controller_Template
{
public function before()
{
parent::before();
$this->template->header = 'header';
$this->template->footer = 'footer';
$this->template->content = 'content';
$this->template->sidebar = 'sidebar';
$this->template->css = array();
$this->template->js = array();
// Title
$this->template->title = '';
// WordPressかどうかの判定を追加する
$this->template->is_wp = false;
}
public function after($response)
{
$response = parent::after($response);
if ($this->response_status == '404')
{
$this->template->title = 'ページが見つかりませんでした';
$this->template->content = View::forge('common/404');
}
return $response;
}
}
pages.php
これに通常のページを作成していくコントローラーを書いていきます。
<?php
/**
* The Pages Controller.
*
* @package app
* @extends Controller_Base
*/
class Controller_Pages extends Controller_Base
{
public function before()
{
parent::before();
}
/**
* Top page.
*/
public function action_index()
{
$this->template->title = 'トップページ';
$this->template->content = View::forge('pages/top');
}
/**
* About page.
*/
public function action_about()
{
$this->template->title = 'ABOUT';
$this->template->content = View::forge('pages/about');
}
/**
* 404 page.
*/
public function action_404()
{
$this->template->title = 'ページが見つかりませんでした。';
$this->template->content = View::forge('common/404');
}
}
posts.php(WordPressの投稿)
このコントローラーにWordPressの投稿を取得できるようにします。
<?php
/**
* The Posts Controller.
*
* WordPressの投稿となる部分のController
*
* @package app
* @extends Controller_Base
*/
class Controller_Posts extends Controller_Base
{
public function before()
{
// この設定がないと、ショートコード系のプラグインなどが動きません。
define('WP_USE_THEMES', true);
// WordPressを処理するためにwp-blog-header.phpを読み込ませます。
require(DOCROOT.'wp/wp-blog-header.php');
parent::before();
$this->template->is_wp = true;
}
/**
* 投稿のメインループ
*/
public function action_index()
{
$this->template->title = wp_get_document_title();
// メインループ時のプレビュー
if (is_preview() and is_user_logged_in()) {
$this->template->content = View::forge('preview/index');
return;
}
$this->template->sidebar = View::forge('posts/sidebar');
$this->template->content = View::forge('posts/index');
}
/**
* 投稿の詳細ページ
*
* @param $id 投稿ID
*/
public function action_detail($id)
{
// 公開されているかチェックしてから表示を行う
if (get_post_status($id) === 'publish')
{
// $this->template->title = wp_get_document_title();
$this->template->title = wp_get_document_title();
$this->template->sidebar = View::forge('posts/sidebar');
$this->template->content = View::forge('posts/index');
return;
}
// publish以外は404を返す
$this->response_status = '404';
}
}
page.php(WordPressの固定ページ)
固定ページは、beforeでWordPressのコードを読み込むとエラーになるので、ページが存在していれば、読み込むように処理をします。
<?php
/**
* The WordPress Page Controller.
*
* WordPressの固定ページ側のController
*
* @package app
* @extends Controller_Base
*/
class Controller_Page extends Controller_Base
{
public function before()
{
parent::before();
}
/**
* 固定ページの表示
*/
public function action_index($name)
{
// セグメントから固定ページのデータを取得する
$page = get_page_by_path($name);
if ( ! is_null($page))
{
// 投稿が公開されていたら固定ページの内容を表示する
if (isset($page->ID) and get_post_status($page->ID) === 'publish')
{
define('WP_USE_THEMES', true);
require(DOCROOT.'wp/wp-blog-header.php');
$title = wp_get_document_title();
$this->template->is_wp = true;
$this->template->title = $title;
$this->template->sidebar = 'sidebar';
$this->template->content = 'content_index_page';
return;
}
}
// WordPress側でプレビュー表示した場合のビューを変更する
if (is_preview() and is_user_logged_in()) {
define('WP_USE_THEMES', true);
require(DOCROOT.'wp/wp-blog-header.php');
$this->template->is_wp = true;
$this->template->title = wp_get_document_title();
$this->template->content = 'content_detail_page';
return;
}
$this->response_status = '404';
}
}
ここまで作成したら、一通り動くようになっていると思います。
また、FuelPHPは1.8以上を推奨します。
あとは、Viewを作ればサイトが作成できます。
MW WP Formプラグインも動くのを確認はしたので、もし動かないってことがあれば、define('WP_USE_THEMES', true)
があるか確認してね。