2012年5月11日金曜日

2012年05月11日 講義062日目

------------------------------1H------------------------------
★symfony
●画像
・image_tag()
<?php echo image_tag("comment/".$comment->getPhoto(),array("width" => 100))?>
●アクセス制御
・管理画面を作る
[nishi@localhost ~]$ cd sf.shonanbbs.com/
[nishi@localhost sf.shonanbbs.com]$ symfony init-app backend

[nishi@localhost sf.shonanbbs.com]$ symfony init-mod backend default
・/web/backendの作成
/web/backend/に
/web/backend.php
/web/backend_dev.php
を移動
・フロントコントローラを書き換える
<?php
#パスを/web/backendになるように変更↓
define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/../..'));
define('SF_APP',         'backend');
#本番環境にするには8,9行目を消して6,7行目のコメントをはずす
#define('SF_ENVIRONMENT', 'prod');
#define('SF_DEBUG',       false);
define('SF_ENVIRONMENT', 'dev');
define('SF_DEBUG',       true);

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

sfContext::getInstance()->getController()->dispatch();
・.htaccessを移動
[nishi@localhost sf.shonanbbs.com]$ cd web
[nishi@localhost web]$ ls
backend  css  front_dev.php  images  index.php  js  robots.txt  sf  uploads
[nishi@localhost web]$ ls -a
.   .htaccess  css            images     js          sf
..  backend    front_dev.php  index.php  robots.txt  uploads
[nishi@localhost web]$ cp .htaccess backend/
[nishi@localhost web]$ ls -a backend
.  ..  .htaccess  backend.php  backend_dev.php
[nishi@localhost web]$
・.htaccessの書き換えてbackend用を作る
☆の行を書き換える
[nishi@localhost web]$ vi backend/.htaccess

  # uncomment the following line, if you are having trouble
  # getting no_script_name to work
☆RewriteBase /backend/

  # we skip all files with .something
  # comment the following 3 lines to allow periods in routes
  RewriteCond %{REQUEST_URI} \..+$
  RewriteCond %{REQUEST_URI} !\.html$
  RewriteRule .* - [L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
☆RewriteRule ^(.*)$ backend.php [QSA,L]
</IfModule>

# big crash from our front web controller
ErrorDocument 500 "<h2>Application error</h2>symfony application failed to start properly"
------------------------------2H------------------------------
★symfony
●backendを作成
・フロントコントローラが違う場合は参照フォルダを別で作る
・/web/backend/cssを作って管理画面用のdefault.cssを置く
・backendのview.ymlの書き換え
default:
  http_metas:
    content-type: text/html

  metas:
    title:        管理画面
    robots:       index, follow
    description:  SHONANBBS管理画面
    keywords:     BBS, 管理
    language:     ja

  stylesheets:    [default]

  javascripts:    []

  has_layout:     on
  layout:         layout

・layout.ymlを書き換える
<!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" xml:lang="en" lang="en">
<head>

<?php include_http_metas() ?>
<?php include_metas() ?>

<?php include_title() ?>

<link rel="shortcut icon" href="/favicon.ico" />

</head>
<body>
<h1>symfony版SHONANBBS 管理画面</h1>
<hr>
<div id="content">

  <div id="contentLeft">
<?php echo $sf_data->getRaw('sf_content') ?>
  </div>

  <div id="contentRight">
  MENU
  <ul>
  <li><a href="/">掲示板トップ</a></li>
  <li><a href="/backend">管理画面トップ</a></li>
  <li><b>ユーザ管理</b>
    <ul class="submenu">
    <li><a href="/backend/member/add">新規追加</a></li>
    <li><a href="/backend/member/list">一覧</a></li>
    </ul>
  </li>
  <li><b>コメント管理</b>
    <ul>
    <li><a href="/backend/comment/list">一覧</a></li>
    </ul>
  </li>
  </ul>

  </div>

</div>

</body>
</html>
・管理画面のデバックモードの表示を良くする
シンボリックリンク作成
[nishi@localhost backend]$ ls
backend.php  backend_dev.php  css
[nishi@localhost backend]$ ln -s /usr/share/pear/data/symfony/web/sf ./sf
[nishi@localhost backend]$ ls
backend.php  backend_dev.php  css  sf
[nishi@localhost backend]$

frontでは/webにシンボリックリンクを作成する
backendにも作らないと色々と表示されないっぽい

[nishi@localhost backend]$ ln -s /usr/share/pear/data/symfony/web/sf ./sf
[nishi@localhost backend]$ ls
backend.php  backend_dev.php  sf←コレ!
[nishi@localhost backend]$
[nishi@localhost backend]$

------------------------------3H------------------------------
------------------------------4H------------------------------
★symfony
●先行教室見学
------------------------------5H------------------------------
★symfony
●管理画面
・ログインモジュールの作成
[nishi@localhost sf.shonanbbs.com]$ symfony init-mod front login
・/apps/backend/config/security.ymlの修正
権限を持っていなかったらログインモジュールに飛ばす処理を書く
all:
  is_secure: on
  credentials: [ member ]
・/apps/front/login/templates/indexSuccess.php
<?php use_helper("Validation")?>

<?php form_tag('login/index')?>
<div class="tb/Data">

<?php  if ($errormsg !=''):?>
<div><?php echo $errormsg;?></div>
<?php endif;?>
<table>
  <tr>
    <th>メールアドレス</th>
    <td<?php if ($sf_request->haserror('mailaddress')):?> style="background-color:#ffcccc;"<?php endif;?>>
      <?php echo input_tag('mailaddress', $mailaddress, array('size'=> 50));?>
      <?php echo form_error('mailaddress');?>
    </td>
  </tr>
  <tr>
    <th>パスワード</th>
    <td<?php if ($sf_request->haserror('password')):?> style="background-color:#ffcccc;"<?php endif;?>>
    <?php echo input_tag('password', $password, array('size' => 50));?>
    <?php echo form_error('password');?>
    </td>
  </tr>
</table>
</div>
<?php echo submit_tag('ログインする');?>
</form>
・/apps/front/modules/login/actions/actions.class.php
<?php use_helper("Validation")?>

<?php form_tag('login/index')?>
<div class="tb/Data">

<?php  if ($errormsg !=''):?>
<div><?php echo $errormsg;?></div>
<?php endif;?>
<table>
  <tr>
    <th>メールアドレス</th>
    <td<?php if ($sf_request->haserror('mailaddress')):?> style="background-color:#ffcccc;"<?php endif;?>>
      <?php echo input_tag('mailaddress', $mailaddress, array('size'=> 50));?>
      <?php echo form_error('mailaddress');?>
    </td>
  </tr>
  <tr>
    <th>パスワード</th>
    <td<?php if ($sf_request->haserror('password')):?> style="background-color:#ffcccc;"<?php endif;?>>
    <?php echo input_tag('password', $password, array('size' => 50));?>
    <?php echo form_error('password');?>
    </td>
  </tr>
</table>
</div>
<?php echo submit_tag('ログインする');?>
</form>

------------------------------6H------------------------------
★symfony
●管理画面にログインする
・/apps/front/modules/login/actions/action.class.php
    public function executeIndex()
    {
        $this->mailaddress = $this->getRequestParameter('mailaddress');

        $this->password = $this->getRequestParameter('password');

        if($this->mailaddress !='' && $this->password != '')
        {
            //メールアドレスとパスワードを元にmemberテーブルのインスタンス取得
            $c = new Criteria;
            $c->add(MemberPeer::MAILADDRESS, $this->mailaddress);
            $c->add(MemberPeer::PASSWORD, $this->password);
            $member = MemberPeer::doSelectOne($c);
            //$member = MemberPeer::retrieveByPassword($this->mailaddress);

            if($member)
            {
                //ログイン状態にする
                $this->getUser()->setAuthenticated(true);
                $this->getUser()->clearCredentials();
                $this->getUser()->addCredential('member');

                //会員情報をセッションにセット
                $this->getUser()->setAttribute('member_id',$member->getId(),sfConfig::get('sf_session_name'));
                $this->getUser()->setAttribute('nickname', $member->getNickname(), sfConfig::get('sf_session_name'));
                $this->redirect('/backend');
            }
        }
        return sfView::SUCCESS;
        //$this->forward('default', 'module');
    }
・apps/backend/templates/layout.php
ログインしたユーザの名前を表示するようにする
<h1>symfony版SHONANBBS 管理画面</h1>
<?php echo $sf_user->getAttribute('nickname', sfConfig::get('sf_session_name'))?>
さんがログインしています
<hr>
・エラーメッセージを表示
●isset()
apps/login/templates/indexSuccess.php
/apps
<?php  if (isset($errormsg)):?>
<div><?php echo $errormsg;?></div>
<?php endif;?>
-----------------------------memo------------------------------

0 件のコメント:

コメントを投稿