Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
4 replies Latest Post - ‏2012-08-13T13:25:10Z by jtzmsqzr
SystemAdmin
SystemAdmin
13339 Posts
ACCEPTED ANSWER

Pinned topic 398947

‏2012-05-25T15:19:59Z |
398947
Updated on 2012-08-13T13:25:10Z at 2012-08-13T13:25:10Z by jtzmsqzr
  • NewPHPer
    NewPHPer
    1 Post
    ACCEPTED ANSWER

    test

    ‏2012-05-25T15:19:59Z  in response to SystemAdmin
    教程很不错
    不过有些地方还有一点点不够清楚
    很多新手都是第一次接触CakePHP的
    现在CakePHP已经更新到2.x了
  • jtzmsqzr
    jtzmsqzr
    3 Posts
    ACCEPTED ANSWER

    test

    ‏2012-08-12T08:27:13Z  in response to SystemAdmin
    1、今天开始学习cakePHP 2.2.1,因为此样本程序是1.x,我用的是2.2.1,会有所不同,一边学一边改吧;
    2、Demo程序Tor的users数据库生成脚本修改:( CakePHP 2.2.1.)
    由:
    CREATE TABLE 'users' (
    'id' INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    'username' VARCHAR( 40 ) NOT NULL ,
    'password' VARCHAR( 40 ) NOT NULL ,
    'email' VARCHAR( 255 ) NOT NULL ,
    'first_name' VARCHAR( 40 ) NOT NULL ,
    'last_name' VARCHAR( 40 ) NOT NULL ,
    UNIQUE (
    'username' ,,
    'email'
    )
    ) TYPE = MYISAM ;
    改为:主要是'改为`
    CREATE TABLE `users` (
    `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 40 ) NOT NULL ,
    `password` VARCHAR( 40 ) NOT NULL ,
    `email` VARCHAR( 255 ) NOT NULL ,
    `first_name` VARCHAR( 40 ) NOT NULL ,
    `last_name` VARCHAR( 40 ) NOT NULL ,
    UNIQUE (
    `username` ,
    `email`
    )
    ) ENGINE=MyISAM
    3、原register.ctp用不了,代以add.ctp,子目录也从users改为user:
    Add User
    <?php
    echo $this->Form->create('User');
    echo $this->Form->input('username');
    echo $this->Form->input('password',array('type' => 'password'));
    echo $this->Form->input('email');
    echo $this->Form->input('first_name');
    echo $this->Form->input('last_name');

    echo $this->Form->end('添加');
    ?>
    user.php的内容改为:
    <?php
    class User extends AppModel
    {
    public $name = 'User';
    }
    ?>
    users_controller.php改为userscontroller.php,内容改为:
    <?php
    class UsersController extends AppController
    {
    public $name = 'User';
    public $helpers = array('Html', 'Form');

    public $components = array('Session');
    public function add()
    {
    if($this->request->is('post') )
    { if($this->User->save(($this->request->data)) )
    {
    $this->Session->setFlash('新用户创建完成');
    //$this->redirect(array('action' => 'index'));
    $this->redirect(array('action' => 'register'));
    } else {
    $this->Session->setFlash('创建用户出错');
    }
    }
    }
    }
    ?>
  • jtzmsqzr
    jtzmsqzr
    3 Posts
    ACCEPTED ANSWER

    test

    ‏2012-08-12T13:15:16Z  in response to SystemAdmin
    knownusers.ctp内容改为:

    <?php
    echo $this->html->tableHeaders(array_keys($knownusers[0]));

    foreach ($knownusers as $thisuser)
    {
    echo $this->html->tableCells($thisuser);
    }

    ?>

    user.php的内容:
    <?php
    class User extends AppModel
    {
    public $name = 'User';
    var $validate = array (
    'username' => array (
    'rule' => '/^a-z0-9{6,40}$/i',
    'message' => 'This field must have between 6 and 40 alphanumeric characters.'
    ),
    'password' => array (
    'rule' => '/^a-z0-9{6,40}$/i',
    'message' => 'This field must have between 6 and 40 alphanumeric characters.'
    ),
    'email' => array(
    'rule' => 'email',
    'message' => 'Please supply a valid email address.'
    )
    );

    }
    ?>
    清单 26. 新的用户名输入行,由:
    echo $form->input('username', array('after' => $form->error
    ('username_unique', 'The username is taken. Please try again.')));
    改为:
    echo $this->Form->input('username', array('after' => $this->Form->error
    ('username_unique', 'The username is taken. Please try again.')));
  • jtzmsqzr
    jtzmsqzr
    3 Posts
    ACCEPTED ANSWER

    test

    ‏2012-08-13T13:25:10Z  in response to SystemAdmin
    第 2 部分: 用 CakePHP 打造更大更好的站点
    清单 4. 创建保存产品信息的表:
    CREATE TABLE products (
    id INT( 10 ) NOT NULL AUTO_INCREMENT ,
    title VARCHAR( 255 ) NOT NULL ,
    dealer_id INT( 10 ) NOT NULL ,
    description blob NOT NULL ,
    PRIMARY KEY (id)
    ) ENGINE = MYISAM ;

    CREATE TABLE dealers (
    id INT( 10 ) NOT NULL AUTO_INCREMENT ,
    title VARCHAR( 255 ) NOT NULL ,
    PRIMARY KEY (id)
    ) ENGINE = MYISAM ;

    由于CakePHP 2.x的Auth组件的功能非常强大,我们引入了该组件,对UsersController.php、User.php作了些修改:
    1、UsersController.php:
    public $components = array('Auth','Acl');

    ...

    public function login() {
    if ($this->request->is('post')) {
    if ($this->Auth->login()) {
    //return $this->redirect($this->Auth->redirect());
    return $this->redirect('/products');
    } else {
    $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
    }
    }
    }

    function register()
    {
    if (!empty($this->data))
    {
    //$this->data =md5($this->data);
    if ($this->User->save($this->data))
    {
    $this->Session->setFlash('Your registration information
    was accepted: '.$this->data);
    $this->Session->write('user', $this->data);
    $parent = $this->Acl->Aro->findByAlias('Users');
    $this->Acl->Aro->create(array(
    'alias' => $this->data,
    'model' => 'User',
    'foreign_key' => $this->User->id,
    'parent_id' => $parent)
    );
    $this->Acl->Aro->save();
    $this->redirect(array('action' => 'login'), null, true);
    } else {
    $this->data = '';
    $this->Session->setFlash('There was a problem saving
    this information');
    }
    }
    }

    public function beforeFilter ()
    {
    parent::beforeFilter();
    $this->Auth->allow('register');
    }
    2、User.php:
    public function beforeSave($options = array()) {
    $this->data = AuthComponent::password($this->data);
    return true;
    }