cake css

cssやjsはwebrootに入れる
app/webroot/css/styles.css

body{
	padding: 0;
	margin: 0;
	font-size: 16px;
	font-family: Verdana, sans-serif;
}

.container {
	width: 380px;
	margin: 0 auto;
}

h1 {
	font-size: 16px;
	border-bottom: 1px solid #ccc;
	padding-bottom: 5px; 
}

ul > li {
	padding: 3px 0;
}

あれ、すこし楽しくなってきましたね。

titleはapp/src/View/Template/Posts/index.ctp 内で、$this->assignという書き方をする

<?php
	$this->assign('title', 'Blog Posts');
?>

<h1>Blog Posts</h1>

<ul>
	<?php foreach($posts as $post):?>
		<li><?= h($post->title); ?></li>
	<?php endforeach; ?> 
</ul>

view.ctp

<?php
	$this->assign('title', 'Blog Detail');
?>

<h1><?= $this->Html->link('Back', ['action'=>'index'], ['class'=>['pull-right','fs12']]); ?> 
<?= h($post->title); ?></h1>

<p><?= nl2br(h($post->body)); ?></p>

cake view

src/Template/Layout/default.ctp

なるほど、これは便利ですね。

<?php
/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         0.10.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */

$cakeDescription = 'CakePHP: the rapid development php framework';
?>
<!DOCTYPE html>
<html>
<head>
    <?= $this->Html->charset() ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        <?= $cakeDescription ?>:
        <?= $this->fetch('title') ?>
    </title>
    <?= $this->Html->meta('icon') ?>

    <?= $this->Html->css('base.css') ?>
    <?= $this->Html->css('cake.css') ?>

    <?= $this->fetch('meta') ?>
    <?= $this->fetch('css') ?>
    <?= $this->fetch('script') ?>
</head>
<body>
    <nav class="top-bar expanded" data-topbar role="navigation">
        <ul class="title-area large-3 medium-4 columns">
            <li class="name">
                <h1><a href=""><?= $this->fetch('title') ?></a></h1>
            </li>
        </ul>
        <div class="top-bar-section">
            <ul class="right">
                <li><a target="_blank" href="https://book.cakephp.org/3.0/">Documentation</a></li>
                <li><a target="_blank" href="https://api.cakephp.org/3.0/">API</a></li>
            </ul>
        </div>
    </nav>
    <?= $this->Flash->render() ?>
    <div class="container clearfix">
        <?= $this->fetch('content') ?>
    </div>
    <footer>
    </footer>
</body>
</html>

レイアウトをカスタマイズします。
src/Template/my_layou.ctp

<!DOCTYPE html>
<html>
<head>
    <?= $this->Html->charset() ?>
    <title>
        <?= $this->fetch('title') ?>
    </title>
    <?= $this->Html->css('styles.css') ?>
</head>
<body>
        <section class="container">
        <?= $this->fetch('content') ?>
        </section>

</body>
</html>

controllerでレイアウトを指定します。
PostsController.php

<?php

namespace App\Controller;

class PostsController extends AppController
{
	public function  index()
	{
		$this->viewBuilder()->layout('my_layout');
		$posts = $this->Posts->find('all');
		$this->set('posts', $posts);
	}
}

なるほど、すこし見えてきましたね。

cake routing

なぜかroutingの設定は好きですね。

app/config/routes.php

Router::scope('/', function (RouteBuilder $routes) {
    /**
     * Here, we are connecting '/' (base path) to a controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, src/Template/Pages/home.ctp)...
     */
    // $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $routes->connect('/', ['controller' => 'Posts', 'action' => 'index']);

actionがなぜindexなのでしょう???

controllerのsqlのorder byは以下のように書くようです。

$posts = $this->Posts->find('all')->order(['title' => 'DESC']);

SELECT * FROM posts LIMIT 10;は

$posts = $this->Posts->find('all')->limit(2);

ですね。

<?php

// /post/index
// /(controller)/(action)/(options)

namespace App\Controller;

class PostsController extends AppController
{
	public function  index()
	{
		// modelからデータを取得し、postsに入れる
		$posts = $this->Posts->find('all')->order(['title' => 'DESC'])
		->limit(2)
		->where(['title like' => '%3']);
		$this->set('posts', $posts);
	}
?>

cake Controllerを作成

テーブル名 + Controller.php
app/src/Controller
PostsController.php

modelからviewへの受け渡しを記載

<?php
// /post/index
// /(controller)/(action)/(options)
namespace App\Controller;

class PostsController extends AppController
{
	public function  index()
	{
		// modelからデータを取得し、postsに入れる
		$posts = $this->Posts->find('all');
		$this->set('posts', $posts);
	}
}
?>

さーバーで見てみると、あれ、小文字になってますね。。

cake Viewを作成

App/src/Template/

テーブル名のフォルダを作成

index.ctpファイルを作成
App/src/Template/Post/index.ctp

テーブルの配列がテーブル名で渡される。例えば、tableがteasなら、viewには、$teasでレコードがobjectで渡される。すげー。

<h1>Blog Posts</h1>

<ul>
	<?php foreach($posts as $post):?>
		<li><?= h($post->title); ?></li>
	<?php endforeach; ?> 
</ul>

cake DBtableのModelを作成

テーブル名は複数形
src/Model/Table/
HogesTable.php

ここでは
PostsTable.php

<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class PostsTable extends Table
{
	public function initialize(array $config)
	{
		$this->addBehavior('Timestamp');
	}
}

やばい、完全に眠くなってきた。

composerを入れてcakeをインストール

まずローカルにcomposerを入れます。

curl -sS https://getcomposer.org/installer | php

続いて、cakeをインストール

php composer.phar create-project --prefer-dist cakephp/app myapp

cd myapp

サーバーを立てます。

bin/cake server -H 192.168.33.10 -p 8000

準備が出来ました。

DBの設定
config/app.php

'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            /**
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',
            'username' => 'my_app',
            'password' => 'secret',
            'database' => 'my_app',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,

mvcをつくる
bin/cake bake all posts
(※postsはDBのテーブル名)

そして、192.168.33.10:8000/posts をたたくと、DBのテーブルのカラム・レコード一覧が表示される。

create table cake.posts(
	id int unsigned auto_increment primary key,
	title varchar(255),
	body text,
	created datetime default null,
	modified datetime default null
);

ImageJPEGとファイルパーミッション

こちらは画像生成が可能で

ImageJPEG($out, '$hoge');

こちらは出来ないので、何故か紋々としていたら、

    
ImageJPEG($out, 'img/'.$hoge.'');  

フォルダのパーミッションが
RWX
R X
R X
755 になっていたからですね。

RWX
RWX
RWX
777 に変更したところ、上手くいきました。

危ない危ない、もう少しでやけ酒(現実逃避)コースでした。