dqq3623 2014-02-17 13:15
浏览 52
已采纳

在zend框架2中的模型中获取数据库适配器

I am a zf1 developer. I started zf2. I am creating a Authentication module. I created a Auth class as mentioned in the doc

<?php
namespace Application\Model;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;

class Myauth implements AdapterInterface {

    /**
     * Sets username and password for authentication
     *
     * @return void
     */
    public function __construct($username, $password) {


        // Configure the instance with constructor parameters...
        $authAdapter = new AuthAdapter($dbAdapter,
        'users',
        'username',
        'password'
        );

        $authAdapter
        ->setTableName('users')
        ->setIdentityColumn('username')
        ->setCredentialColumn('password');


        $result = $authAdapter->authenticate();

        if (!$result->isValid()) {
            // Authentication failed; print the reasons why
            foreach ($result->getMessages() as $message) {
                echo "$message
";
            }
        } else {
            // Authentication succeeded
            // $result->getIdentity() === $username
        }

    }
}

Issue1 : How to get $dbAdapter here? Issue2 : Is this correct way to create auth module?

  • 写回答

2条回答 默认 最新

  • douyi4544 2014-02-17 14:23
    关注

    I have a couple things to say:

    1. About Database Adapter

    This link shows you how to configure database adapter.

    In config/autoload/global.php:

     return array(
     'db' => array(
         'driver'         => 'Pdo',
         'dsn'            => 'mysql:dbname=zf2tutorial;host=localhost',
         'driver_options' => array(
             PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         ),
     ),
     'service_manager' => array(
         'factories' => array(
             'Zend\Db\Adapter\Adapter'
                     => 'Zend\Db\Adapter\AdapterServiceFactory',
         ),
     ),
    );
    

    In config/autoload/local.php:

     return array(
         'db' => array(
             'username' => 'YOUR USERNAME HERE',
             'password' => 'YOUR PASSWORD HERE',
         ),
     )
    

    Now, from ServiceLocatorAware classes, you can get Database Adapter as

    $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    

    2. About Creating Authentication

    Dude, why reinvent the square wheel? As mentioned here, ZfcUser is built to be a foundation for a very large percentage for Zend Framework 2 applications.

    Nearly anything is customizable as mentioned here. A lot of modules are available such as ScnSocialAuth which have dependancy on ZfcUser and are really awesome.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R