Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use it? Any demo? #27

Open
mikguo opened this issue Sep 15, 2015 · 9 comments
Open

how to use it? Any demo? #27

mikguo opened this issue Sep 15, 2015 · 9 comments

Comments

@mikguo
Copy link

mikguo commented Sep 15, 2015

when I insert
$di['dispatcher'] = function() use ($di) {
$eventsManager = $di->getShared('eventsManager');
$security = new \Phalcon\UserPlugin\Plugin\Security($di);
$eventsManager->attach('dispatch', $security);

        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $dispatcher->setEventsManager($eventsManager);
        return $dispatcher;
    };

to my project, it's report fatal error: Call to a member function hasRememberMe() on a non-object.
I need setAuth first? Could you uploads any demo how to use it? Thanks.

@mizterp
Copy link
Contributor

mizterp commented Sep 15, 2015

At the moment there is a bug with the "master" repo, so you'll need to grab v2.0 via composer

@mikguo
Copy link
Author

mikguo commented Sep 15, 2015

sorry, it's still report the error.

2015-09-15 12:38 GMT+08:00 Mike Guo [email protected]:

ok, thanks.

2015-09-15 12:34 GMT+08:00 Carl [email protected]:

At the moment there is a bug with the "master" repo, so you'll need to
grab v2.0 via composer


Reply to this email directly or view it on GitHub
#27 (comment)
.

@mizterp
Copy link
Contributor

mizterp commented Sep 15, 2015

Could you share your project via github?

My services.php looks something like this:

$di = new FactoryDefault();

//Make config settings available
$di->set('config', $config, true);

/**
 * Plugging the PhalconUserPlugin
 */
$di['dispatcher'] = function() use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    $security = new \Phalcon\UserPlugin\Plugin\Security($di);
    $eventsManager->attach('dispatch', $security);
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
};
/**
 * Register Auth, ACL and Mail services used by PhalconUserPlugin
 */
$di['auth'] = function(){
    return new \Phalcon\UserPlugin\Auth\Auth();
};
$di['acl'] = function() {
    return new \Phalcon\UserPlugin\Acl\Acl();
};
$di['mail'] = function() {
    return new \Phalcon\UserPlugin\Mail\Mail();
};

@mikguo
Copy link
Author

mikguo commented Sep 15, 2015

my project uses multiple model, and in Model.php, I insert the code:

public function registerServices(DiInterface $di)
{
/**
* Read configuration
*/
$config = include APP_PATH . "/apps/backend/config/config.php";

/**
 * Setting up the view component
 */
$di['view'] = function () use ($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            $compiler = $volt->getCompiler();
            $compiler->addExtension(new PhpFunctionExtension());


            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));
    // Disable several levels
    $view->disableLevel(
        array(
            View::LEVEL_LAYOUT      => true,
            View::LEVEL_MAIN_LAYOUT => true
        )
    );
    return $view;
};

/**
 * Database connection is created based in the parameters defined

in the configuration file
*/
$di['db'] = function () use ($config) {
return new DbAdapter($config->database->toArray());
};

/**
 * Plugging the PhalconUserPlugin
 */
$di['dispatcher'] = function() use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    $security = new \Phalcon\UserPlugin\Plugin\Security($di);
    $eventsManager->attach('dispatch', $security);

    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
};

$di['auth'] = function(){
    return new \Phalcon\UserPlugin\Auth\Auth();
};

$di['acl'] = function() {
    return new \Phalcon\UserPlugin\Acl\Acl();
};

$di['mail'] = function() {
    return new \Phalcon\UserPlugin\Mail\Mail();
};

$di->set('config', $config);

}

but when I insert these code, it's report:
Fatal error: Call to a member function hasRememberMe() on a non-object in
D:\myprojectpath\apps\user\Plugin\Security.php on line 84

2015-09-15 15:20 GMT+08:00 Carl [email protected]:

Could you share your project via github?

My services.php looks something like this:

$di = new FactoryDefault();//Make config settings available$di->set('config', $config, true);/** * Plugging the PhalconUserPlugin /$di['dispatcher'] = function() use ($di) { $eventsManager = $di->getShared('eventsManager'); $security = new \Phalcon\UserPlugin\Plugin\Security($di); $eventsManager->attach('dispatch', $security); $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher;};/* * Register Auth, ACL and Mail services used by PhalconUserPlugin */$di['auth'] = function(){ return new \Phalcon\UserPlugin\Auth\Auth();};$di['acl'] = function() { return new \Phalcon\UserPlugin\Acl\Acl();};$di['mail'] = function() { return new \Phalcon\UserPlugin\Mail\Mail();};


Reply to this email directly or view it on GitHub
#27 (comment)
.

@mikguo
Copy link
Author

mikguo commented Sep 16, 2015

Now I edit the code like:

/**
         * Plugging the PhalconUserPlugin
         */
        $di['auth'] = function(){
            return new \Phalcon\UserPlugin\Auth\Auth();
        };

        $di['acl'] = function() {
            return new \Phalcon\UserPlugin\Acl\Acl();
        };

        $di['mail'] = function() {
            return new \Phalcon\UserPlugin\Mail\Mail();
        };

        $di['dispatcher'] = function() use ($di) {
            $eventsManager = new \Phalcon\Events\Manager();
            $security = new \Phalcon\UserPlugin\Plugin\Security($di);
            $security->setView($di['view'])->setAuth($di['auth']);
            $eventsManager->attach('dispatch', $security);

            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            $dispatcher->setEventsManager($eventsManager);
            return $dispatcher;
        };

the error was fixed. seems like that Security.php lost initialize()?

@mizterp
Copy link
Contributor

mizterp commented Sep 16, 2015

Did you remember to configure the 'pup' array in your config?

It should look something like this:

//Example of settings used by PhalconUserPlugin
    'pup' => array(//[P]halcon[U]ser[P]lugin
        'redirect' => array(
            'success' => 'user/profile', //...redirect to this controller/action on success
            'failure' => 'user/login' //... redirect here on auth failure
        ),
        'resources' => array(
            'type' => 'public',
            'resources' => array(
                '*' => array( // All except
                    'user' => array('account', 'profile') //...the actions "account" and "profile" are private
                ),
            )
        ),
    )

@mikguo
Copy link
Author

mikguo commented Sep 16, 2015

Yes, I have add the config values when start to use it.

@mzf
Copy link

mzf commented Jan 21, 2016

Awesome! This thread help me a lot

@bnamnguyen
Copy link

$security->setView($di['view'])->setAuth($di['auth']);

This solved my problem, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants