knaka Tech-Blog

AI, IoT, DIYエレクトロニクス, データサイエンスについて投稿予定です。

Codeigniter 初級編、フォーム追加など。 #php #web

index:

概要:
Codeigniter3 php 軽量フレームワークの導入編的な内容となります。

環境とか

CodeIgniter-3.1.10
php 5.6
mysql

追加手順

https://codeigniter.com/
の、download おす。
zip取得、解凍する

apche の場合は、htdocs 下に設置

application/
下に、ユーザーコードを配置するようです。

・DB設定
事前に、mysql DB/table を作成しておく。

config/database.php

username、password、database
 を、任意に設定。

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'user123',
	'password' => 'pass123',
	'database' => 'db123',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

ルーティング

準備ですが、
ルーティング設定に必用.htaccess
が、含まれてなかったので。下記を追加して配置
しておきます。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

フォーム追加

参考:
https://codeigniter.com/user_guide/tutorial/news_section.html

https://codeigniter.com/user_guide/tutorial/create_news_items.html


・リスト
コントローラ:
controllers/news.php
https://github.com/kuc-arc-f/codeig/blob/master/application/controllers/news.php


f:id:knaka0209:20190313150146p:plain

・追加、画面
views/news/create.php
https://github.com/kuc-arc-f/codeig/tree/master/application/views/news

f:id:knaka0209:20190313150336p:plain

・ルーティング追加
config/routes.php

$route['news/edit/(:any)'] = 'news/edit/$1';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';


・URLは、下記で開けました。(ローカルの場合です。 )

http://127.0.0.1/プロジェクト名/news/

下記でも、開きました、
http://127.0.0.1/プロジェクト名/index.php/news/

参考の設定 例など

php5.6 です。
github.com