Last active
February 24, 2020 06:04
-
-
Save murachi1208/47f85d0a8ccd8a8a5000adeb5b4ece5d to your computer and use it in GitHub Desktop.
Slim 3 Framework でMySQLとテンプレつかってみる。その2 ref: https://qiita.com/murachi1208/items/bef385f76d055388179f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table class="pure-table"> | |
<tr> | |
<th>id</th> | |
<th>title</th> | |
<th>name</th> | |
</tr> | |
<?php foreach($data['books'] as $test): ?> | |
<tr> | |
<td><?=$test['id'] ?></td> | |
<td><?=$test['title'] ?></td> | |
<td><?=$test['name'] ?></td> | |
</tr> | |
<?php endforeach; ?> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MySQL | |
$container['db'] = function ($c) { | |
$settings = $c->get('settings')['db']; | |
$pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'] . ";port=" . $settings['port'], | |
$settings['user'], $settings['pass']); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
return $pdo; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo yum --enablerepo=epel,remi,remi-php70 install php70 php70-php-mcrypt php70-php-mbstring php70-php-fpm php70-php-gd php70-php-pecl-xdebug php70-php-pecl-redis php70-php-pecl-imagick-devel php70-php-pecl-imagick php70-php-mysqlnd php70-php-intl php70-php-bcmath php70-php-pecl-zip php70-php-xmlrpc php70-php-xml php70-php-pecl-http php70-php-pecl-http-devel php70-php-opcache phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ composer create-project slim/slim-skeleton slim3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(3) { [0]=> array(4) { ["id"]=> string(1) "1" ["title"]=> string(15) "双子の帝國" ["name"]=> string(24) "ふたごのていこく" ["price"]=> string(3) "300" } [1]=> array(4) { ["id"]=> string(1) "2" ["title"]=> string(15) "進撃の巨人" ["name"]=> string(7) "singeki" ["price"]=> string(3) "500" } [2]=> array(4) { ["id"]=> string(1) "3" ["title"]=> string(21) "となりの関くん" ["name"]=> string(24) "となりのせきくん" ["price"]=> string(5) "50000" } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id title name | |
1 双子の帝國 ふたごのていこく | |
2 進撃の巨人 singeki | |
3 となりの関くん となりのせきくん |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
slim3 | |
├── composer.json | |
├── composer.lock | |
├── classes ← こいつだけ作成した | |
├── logs | |
├── public | |
│ └── index.php | |
├── src | |
│ ├── dependencies.php | |
│ ├── middleware.php | |
│ ├── routes.php | |
│ └── settings.php | |
├── templates | |
│ └── index.phtml | |
└── vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `book` ( | |
`id` int(11) NOT NULL, | |
`title` varchar(64) DEFAULT NULL, | |
`name` varchar(32) DEFAULT NULL, | |
`price` int(5) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO `book` VALUES (1,'双子の帝國','ふたごのていこく',300),(2,'進撃の巨人','singeki',500),(3,'となりの関くん','となりのせきくん',50000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class load | |
spl_autoload_register(function ($classname) { | |
require (__DIR__ . "/../classes/" . $classname . ".php"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Mapper { | |
protected $db; | |
public function __construct($db) { | |
$this->db = $db; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$app->get('/book_api2', function ($request, $response, $args) { | |
$mapper = new TestMapper($this->db); | |
$test = $mapper->getTests(); | |
$response = $this->renderer->render($response, "book_api.phtml", ["books" => $test]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DataBase(MySQL) settings | |
'db' => [ | |
'host' => 'hostname', | |
'port' => '3306', | |
'user' => 'user', | |
'pass' => 'passwd', | |
'dbname' => 'test', | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestMapper extends Mapper | |
{ | |
public function getTests() { | |
$sql = "select * from book"; | |
$stmt = $this->db->query($sql); | |
$results = []; | |
while($row = $stmt->fetch()) { | |
$results[] = $row; | |
} | |
return $results; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment