• []
  • []
  •  
  • del.icio.us 
  •  
  • Yahoo!֥åޡϿ 

cakePHPでアプリケーション作成

PHP 求人 募集 Symfony CakePHP Smarty Ruby on Rails

cakePHPにビジネスロジックを追加する

続いて動作システム部分を追加していく。

Controller修正

Controllerの内容を変更する。

<?php
class UsersController extends AppController {
 
        var $name = 'Users';
        var $helpers = array('Html', 'Form' );
   function index() {
   }
 
    function login()
    {
        //Don't show the error message if no data has been submitted.
        $this->set('error', false); 
 
        // If a user has submitted form data:
        if (!empty($this->data))
        {
            // First, let's see if there are any users in the database
            // with the username supplied by the user using the form:
 
            $someone = $this->User->findByUsersLogin($this->data['User']['username']);
 
            // At this point, $someone is full of user data, or its empty.
            // Let's compare the form-submitted password with the one in 
            // the database.
 
            if(!empty($someone['User']['users_password']) 
              && $someone['User']['users_password'] == md5( trim( $this->data['User']['password'] ) ) )
            {
                // Note: hopefully your password in the DB is hashed, 
                // so your comparison might look more like:
                // md5($this->data['User']['password']) == ...
 
                // This means they were the same. We can now build some basic
                // session information to remember this user as 'logged-in'.
 
                $this->Session->write('User', $someone['User']);
 
                // Now that we have them stored in a session, forward them on
                // to a landing page for the application. 
 
                $this->redirect('/clients');
            }
            // Else, they supplied incorrect data:
            else
            {
                // Remember the $error var in the view? Let's set that to true:
                $this->set('error', true);
            }
        }
    }
    function logout()
    {
        // Redirect users to this action if they click on a Logout button.
        // All we need to do here is trash the session information:
 
        $this->Session->delete('User');
 
        // And we should probably forward them somewhere, too...
 
        $this->redirect('/');
    }
 
}
?>

View追加

Controller名user, Action名loginでViewを追加する。

<h2>login</h2>
<?if ($error): ?>
<p>The login credentials you supplied could not be recognized. Please try again.</p>
<? endif; ?>
 
<form action="<?php echo $html->url('/users/login'); ?>" method="post">
<div>
    <label for="username">Username:</label>
    <?php echo $html->input('User/username', array('size' => 20)); ?>
</div>
<div>
    <label for="password">Password:</label>
    <?php echo $html->password('User/password', array('size' => 20)); ?>
</div>
<div>
    <?php echo $html->submit('Login'); ?>
</div>
</form>

データ追加

下記のSQLを発行。

INSERT INTO users(users_login, users_password, users_firstname, users_lastname)
 VALUES( 'testuser', md5('hogehoge'), 'do', 'zo' )
;

Webでアクセス

Webからアクセスする。 http://dozo.matirx.jp/users/login

username, passwordの入力画面が表示され、
入力してsubmitするとログイン認証が行われる。
サンプルなのでセキュリティ的にはもう一つだが、
良くある会員制サービスの基本となるところなので、
キチンと押さえておくこと。


framework/cakephp/ビジネスロジック.txt · 最終更新: 2007/06/03 19:40 by dozo