前提条件
ファイルダウンロード
下記から最新のzipをダウンロードする。
https://www.codeigniter.com/download
下記は3.06で動作確認。
zip展開した中身をapacheのDocumentRootにコピー
ブラウザでのアクセスはこちら
下記のURLにアクセスする。(localhostで実行している場合)
http://localhost/index.php/hello
Controllerのみ
クラスファイル作成
下記のとおりhello.phpファイルを作成し、
application\controllers
配下に配置する。
hello.php
<?php
class Hello extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
Controller/View
hello.phpを書き換える。
hello.php
<?php
class Hello extends CI_Controller {
public function index()
{
$data = array(
'title' => 'hello',
'message' => 'Hello World'
);
$this->load->view('helloview', $data);
}
}
?>
helloview.phpを作成し、
application\viewsに配置する。
helloview.php
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $message;?></h1>
</body>
</html>
Controller/View/Model
hello.phpを書き換える。
hello.php
<?php
class Hello extends CI_Controller {
public function index()
{
$this->load->model('Hellomodel');
$data = $this->Hellomodel->getHello();
$this->load->view('helloview', $data);
}
}
?>
viewは変更なし
hellomodel.phpを作成し、
application\modelsに配置する。
hellomodel.php
<?php
class Hellomodel extends CI_Model {
function __construct(){
parent::__construct();
}
function getHello(){
$data = array(
'title' => 'hello',
'message' => 'Hello World'
);
return $data;
}
}
?>