ܻؤ1顪30奵꡼ޥܥ

ITȤƯ30奵꡼ޥΥ饤ߥ󥰵Ͽ

CakePHP

20100630211430 褯ꤽʤʤ󤸤Υ̡
󤿤ܥȡID/PASSϤեࡣ

󤿤ϤޤɡȤꤢϿޤ
ꥢΥȥIP¤Ϥޤ礦äƤ󤸡


ɸAuthݡͥȤȡktai饤֥ȤäƤޤ

UIDꤷƤС֤󤿤פǤơƤʤID/PASSϤUIDꤹ뤳ȤǤȤǽ
ϥȥåץڡǹԤǤ
CakeΥС1.3

APP/controllers/top_controller.php

class TopController extends AppController
{
  var $name = 'Top';
  var $uses = 'User';
  var $helpers = array('Ktai','Session','Form');
  var $components = array('Ktai','Session','Auth');

  function beforeFilter() {
    $this->Auth->loginAction = '/';
    $this->Auth->autoRedirect = false;
    $this->Auth->authorize = 'controller';
    $this->Auth->allow('index');
    parent::beforeFilter();
  }

  function beforeRender() {
    parent::beforeRender();
  }

  function index () {
    if ($user = $this->Auth->user()) {
      if (isset($this->data['add_uid'])) {
        $user['User']['uid'] = $this->Ktai->get_uid();
        $this->User->save($user['User']);
/* 7/14ɵ */
        $user = $this->User->findById($set['id']);
        $this->Auth->login($user);
/* 7/14ɵޤ */
      }
      $this->redirect($this->Auth->redirect());
    } else {
      if (isset($this->data['auto_login'])) {
        if ($user = $this->User->findByUid($this->Ktai->get_uid())) {
          if ($this->Auth->login($user['User'])) {
            $this->redirect($this->Auth->redirect());
          } else {
            $this->Session->setFlash('٤󤿤򤷤Ƥ',null);
          }
        } else {
          $this->Session->setFlash('󤿤꤬Ƥޤ',null);
        }
      }
    }
  }

  function isAuthorized () {
    $user = $this->Auth->user();
    $user['User']['last_login'] = date('Y-m-d H:i:s');
    $this->User->save($user['User']);

    return true;
  }
}

äƤ뤳Ȥϰʲ3
ID/PASSǥơ֤󤿤ꤹפ˥åƤСUIDȤäƥơ֥ͤùࡣ
󤿤ξUID򥭡usersơ֥뤫ǡäƤAuth->loginͤùǥ󤵤롣
isAuthorized()ǥ򹹿

usersơ֥

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(10)      | NO   | PRI | NULL    | auto_increment |
| username    | varchar(100) | NO   |     | NULL    |                |
| password    | varchar(100) | NO   |     | NULL    |                |
| uid         | varchar(100) | NO   |     | NULL    |                |
| flag        | int(1)       | NO   |     | NULL    |                |
| last_login  | datetime     | NO   |     | NULL    |                |
| created     | datetime     | NO   |     | NULL    |                |
| updated     | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+


åȤʤӤޤޤ󤢤뤿ʼdocomoˡΤ˺ʤ褦˥⡣

APP/config/bootstrap.php

if (!isset($_SESSION)) {
  if ($ini_set) {
    ini_set('session.use_trans_sid', 1);
    ini_set('session.serialize_handler', 'php');
    ini_set('session.use_cookies', 0);
  }
}


2010/07/14ɵ
κݤUIDơ֥ɲä硢
$this->Auth->user()Ǽ桼ǡϥåʤΤǺ٥󤷤ʤȤʤߤ
    Υȥ꡼ϤƤʥ֥åޡɲ

ơ֥ΥΥפtinyint(1)ˤơsave褦Ȥͤ

  $set = array('type' => 2);
  $this->Data->create();
  $this->Data->save($set);

type2褦ȤƤɡǡ١Ƥߤ1ˤʤäƤ롪
ä餤äѤǤƤΤǡߤʤäѤϥޤ͡
ʤߤCakePHP1.3Ǥ

Cakephptinyintbooleanˤ餷ʤƤä

CORE/libs/model/datasources/dbo/dbo_mysql.php line474
    if (($col == 'tinyint' && $limit == 1) || $col == 'boolean') {
      return 'boolean';
    }

ϼºθƤߤʤȵŤʤ͡
̵̤˻֤Ȥ줿
    Υȥ꡼ϤƤʥ֥åޡɲ

־

־򰷤饤֥ϤȤޤ
docomoiꥢǡѤ֤̾Ƥޤ
http://labs.unoh.net/2008/08/phpgeomobilejp_converter.html
vendors/Geomobilejp/ˤĤäࡣ

2֤ΰٷ٤ľΥ

򻲹͡
http://www.pahoo.org/e-soul/webtech/php02/php02-21-01.shtm
嵭Υ饤֥Υǡ򤽤Τޤͤù褦ˡäȤ
׻ʬϤΤޤޤǤ


ǡCakePHPΥݡͥȤǻȤäΤǤޤȤޤ
³ɤ
    Υȥ꡼ϤƤʥ֥åޡɲ

ΤǻȤȤХURLȤ
Ķˤäڤؤʤȡ

֡ȯɤǤȤΤ
APP/config/common.php

Ķ˰¸Τ
dev.phpproduction.php
ߤʤ󤸤ˡ

ĶꤹΤ
apacheconfvirtualHost񤤤Ƥ褦ʤȤ
CAKE_CONFIGŪ̾ǡ

SetEnv CAKE_CONFIG dev

APP/controller/app_controller.php

configꤷ
ǡbeforeRendeerconfig˥åȤ
viewǤ$configǻȤ褦ˡ

  function beforeFilter() {
    Configure::load('common');
    if (isset($_SERVER['CAKE_CONFIG'])) {
      Configure::load($_SERVER['CAKE_CONFIG']);
      $this->config = array_merge(Configure::read("common"), Configure::read($_SERVER['CAKE_CONFIG'\
]));
    } else {
      $this->config = Configure::read("common");
    }
  }
  function beforeRender() {
    $this->set("config", $this->config);
  }
    Υȥ꡼ϤƤʥ֥åޡɲ

OpenIDǥ󤷤ơ˥å͡ϿƤ餦褦ʲ̤ǤΥХǡȥ⡣

usersơ֥nickname˥ХǡȤ򤫤


åܤ3ġ

  • ˡʥ˥å͡फ
  • ʸ⤫
  • ΤߤǤϤʤ

ΤߤϤfunction notSpaceǥå

APP/model/user.php

<?php
class User extends AppModel {
    var $name = 'User';
    var $validate = array(
        'nickname' => array(
                            'notSpace' => array(
                                                'rule' => array('notSpace'),
                                                'message' => 'ΤߤϿǤޤ'
                                                ),
                            'between' => array(
                                               'rule' => array('between', 1, 45),
                                               'message' => '1ʸʾϤƤ15ʸޤǡ'
                                               ),
                            'isUnique' => array(
                                                'rule' => 'isUnique',
                                                'message' => 'ǤϿƤޤ'
                                                ),
                            ),
        );
    function notSpace($field=array()) {
      foreach($field as $name => $value){
        if (preg_match("/^( |)+$/", $value)) {
          return false;
        } else {
          return true;
        }
      }
    }
}
?>
    Υȥ꡼ϤƤʥ֥åޡɲ

ȤꤢƥȤǥХ륵ȤǺäƤߤ褦ȡ
¸ΤܤƤ륵ȡʥͥȥˤΥХǺäƤߤ

Ȥ˥ХѤ˵ǽɲäʤɤϤƤ餺
ǥ꡼ä󥭥󥰤ñ̤ˤɽ餤
PCǤZendFrameworkǺäƤ뤱ɡХǤCakePHPäƤȤʰ㤤

2010ǯ118PCȤǥХǤιΡ
1֤η¬

Х


moPV

moUU



Ʊ֤PCǡ


pcPV

pcUU



ͻ


UUߤȡΤޤޤǤPCǤ1/10餤Υ桼褽ʴ
ޡǤ⡢ΥȤ9᤯Υ桼ͥȥ򥿤ʤΤPCȤȤ⤤äƤȤ
΢̤ǥå뤳Ȥ¿
Ĥ1ΥꥢʾСˤưȤ˸Ƥäƹͤȡη̤Ǽ

褯ȥХǤPV1/231/24ȤǤ롣


פ˳ˤϽФˡȤǤäȥͥȥäƤѥ͡


ʤ󤫤ޤǹͻ˳οƤäƤʤʤʤ͡
    Υȥ꡼ϤƤʥ֥åޡɲ

˰³GoogleAnalyticsƳ
ľܽ񤤤Ƥ⤤ɡȤꤢإѡͤù󤿡
vendorˤΤޤޤۤΤʡ

APP/views/helpers/ga.php

Υץ륽إѡѤ˽񤭴

<?php
class GaHelper extends Helper {
  private $GA_ACCOUNT="MO-XXXXXX-X";
  private $GA_PIXEL="/ga.php";

  function getUrl() {
    $url = "";
    $url .= $this->GA_PIXEL . "?";
    $url .= "utmac=" . $this->GA_ACCOUNT;
    $url .= "&utmn=" . rand(0, 0x7fffffff);
    $referer = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : '';
    $query = $_SERVER["QUERY_STRING"];
    $path = $_SERVER["REQUEST_URI"];
    if (empty($referer)) {
      $referer = "-";
    }

    $url .= "&utmr=" . urlencode($referer);
    if (!empty($path)) {
      $url .= "&utmp=" . urlencode($path);
    }
    $url .= "&guid=ON";
    return str_replace("&", "&", $url);
  }
}
?>

APP/controllers/app_controller.php

٤ƤΥڡǽФ顢app_controller

<?php
class AppController extends Controller {
  var $helpers = array('ga');

APP/views/layouts/main.ctp

ᥤΥ쥤Ȥbody˽񤯡

<html>
<head></head>
<body>
   
   
<?php $googleAnalyticsImageUrl = $ga->getUrl(); ?>
<img src="<?php echo $googleAnalyticsImageUrl; ?>" />
</body>
</html>

APP/webroot/ga.php

ȤGAδ̤DLǤga.phpwebroot֤ƽλ
ưƤߤ

    Υȥ꡼ϤƤʥ֥åޡɲ

xml

<?xml version="1.0" encoding="Shift_JIS"?>

ǽ񤯤ȥ顼ˤʤ롣
֤ߤʺǽˤǤĤޤŤ͡
ä餹ǤƤ

ˡϤĤäơ
echoǤ
apache
Ȥ

echoǽФޤ
<?php echo'<?xml version="1.0" encoding="Shift_JIS"?>'."\n"; ?>

DOCTYPEλ

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

3ꥢ̤DOCTYPEʬΥ
http://ma-san.org/2009/10/htmlphpdoctype.html

Content-Typeإåλ

<meta http-equiv="Content-Type" content="application/xhtml+xml ; charset=Shift_JIS" />

apacheǤǤϤʤ󻪤ȤŪˤ褦˥ˡ
app_controller.phpbeforerenderǽϡ
*)Ѿbeforerender()parent:beforerender()˺줺ˡ

APP/controller/app_controller.php

<?php
class AppController extends Controller {
  function beforerender() {
    header('Content-Type: application/xhtml+xml');
  }
}

ߥ졼

docomo
http://www.nttdocomo.co.jp/service/imode/make/content/browser/html/tool2/index.html

softbank
http://creation.mb.softbank.jp/web/web_tool.html

au
http://www.au.kddi.com/ezfactory/tool/ue/
http://web.archive.org/web/*/developer.openwave.com/ja/tools_and_sdk/openwave_mobile_sdk/SDK62K/Openwave_SDK_62K.exe

fireMobileSimulator
http://firemobilesimulator.org/
    Υȥ꡼ϤƤʥ֥åޡɲ

ƥڡˤäɤ߹jsѤȤˡ
ñʤȤɡդ˺줽ʤΤǡ

/path/to/app/webroot/js/foo.jsɤ߹ߤ硣

controller

ȥǤϥإѡθƤӽФ
<?php
class HogeController extends AppController {
  var $name = 'Hoge';
  var $helpers = array('Javascript');

  public function index() {
  }


view

ӥ塼ǤaddScript<head>˵Ҥ롣
($scripts_for_layoutʬ)
<?php $this->addScript($javascript->link('foo')); ?>

ξ˽ϤϤΤޤ
<?php echo $javascript->link('foo'); ?>

layout

쥤ȤϤȤꤢʤ󤸤$scripts_for_layout񤯡
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<?php echo $scripts_for_layout; ?>
</head>
<body>
    Υȥ꡼ϤƤʥ֥åޡɲ

ɤTwitterڤ٤
ʤˤڤΤޤ褯狼Τ
ʬǶ̣Ƥ褦APIȤäƥȺޤ
CakePHPͤơ

ECôäݤߤפ򥭡ɤ˽Ƥߤޤ

Ȥꤢ֤äѤʤߤǺäΤȤ겡ʴǤ


˾ʤ

˾ʤ

åäꡢ⤦äȥꥢ륿Ȳ¿ȻפΤǤܤܤޤ
    Υȥ꡼ϤƤʥ֥åޡɲ

app/vendors/shells/ʲ˥ե

/path/to/app/vendors/shells/foo.php
<?php
class FooShell extends Shell {

  var $uses = array('Hoge');

  function bar(){
    $this->args[0]; /* ޥɥ饤󤫤ΰ */
    /*  */
  }
}
?>


cronǻѤ륱¿ȻפΤǡΤ褦-appappǥ쥯ȥΥѥꡣ
/path/to/cake/console/cake foo bar -app /path/to/app/
    Υȥ꡼ϤƤʥ֥åޡɲ

cakePHPȤäƤߤ롣
PHPե2009εˤǤѼԤ¿äե졼
MVCΥե졼ȤäȤͤˤϤ狼䤹ǥ쥯ȥ깽

ޤǤΥĤĤ顣

ܹpost_typesơ֥ξ

ȥURL: ʣ/post_types/᥽å̾/ѥ᡼
ȥ顧 ʣPostTypesController (extends AppController)
ǥ롧 ñPostType(extends AppModel)
ӥ塼 ʣapp/views/post_types/ƥ᥽å.ctp

ǥ

  • ơ֥μ祭ϡ"id"Ȥեɤǡprimaryauto_increment ǺΤܡ
  • "ǥ̾_id"Ȥեɤǡ¾Υơ֥ǥؤγȤꡣ 㡧users ơ֥ˡ "post_id" ͡posts ơ֥ id 򻲾ȡ "post_type_id" post_types ơ֥ id 򻲾ȡ
  • "created" (DATETIME)ȤեɤǡǡȻ֤ưŪ˵Ͽ
  • "modified" (DATETIME)Ȥեɤǡǡ뤿Ӥ˻֤ưŪ˵Ͽ
  • "updated" (DATETIME)Ȥեɤǡǡ뤿Ӥ˻֤ưŪ˵Ͽ
  • "title"ȤեɤȡΥڡɸΥȥ̾ȤѤ롣
  • "name""title" Ȥեɤȡ黲Ȥ줿ˡʻ꤬ʤСˤͤ쥯ȥܥåʤɤΡ̾פȤƼưŪѤ롣(ȡgenerateList()ΰꤵƤʤˤϤΤ褦ưȤȤˤʤޤ)ɤʤСidͤǥåȹ̾ξޤʤΤνϡmodel_php4.php model_php5.php ˽񤫤Ƥޤ

ӥ塼Ϥɬפʤ᥽åɤξ

<?php
$this->autoRender = false;
?>
졢Хơ֥ɬפʤΡäƻפäƤޤä ⡣

ơ֥̤ѥǥ롢ǥ̤ѥȥ

DBѤʤǥ$useTable = false;
ǥѤʤȥ var $uses = array();ޤ var $uses = nullOK
    Υȥ꡼ϤƤʥ֥åޡɲ

ΥڡΥȥåץ