7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CodeIgniter3でHelloWorld

Posted at

前提条件

ファイルダウンロード

下記から最新の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;
	}
}
?>
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?