robclancy / presenter

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.
MIT License
345 stars 38 forks source link

my presenter functions don't work #11

Closed newtonianb closed 11 years ago

newtonianb commented 11 years ago

I'm trying to get this working but my presenter functions don't work. How do I go about debugging this?

This is how i pass the user object to my view and since I implemented getPresenter it seems that all that I should have to do? (using sentry 2)

$user = Sentry::getUser();
View::share('user', $user);

I've extended my model to

use Illuminate\Auth\UserInterface;
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
use Robbo\Presenter\PresentableInterface;

class User extends SentryUserModel implements PresentableInterface  {
    /**
     * Return a created presenter.
     *
     * @return Robbo\Presenter\Presenter
     */
    public function getPresenter()
    {
        return new UserPresenter($this);
    }
I've changed the config (cartalyst/sentry/config.php) in setting to use my User model
'model' => 'User',
HellPat commented 11 years ago

Looks good for now. Could you post your UserPresenter?

newtonianb commented 11 years ago

Yes here it is

<?php

use Robbo\Presenter\Presenter;

class UserPresenter extends \Presenter
{
    public $resource_name = 'user';

    public function getTest()
    {
        return 'bla';
    }

    public function isActivated()
    {
        if( $this->confirmed )
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public function currentUser()
    {
        if( Auth::check() )
        {
            return Auth::user()->email;
        }
        else
        {
            return null;
        }
    }

    public function displayDate()
    {
        return date('m-d-y', strtotime($this->created_at));
    }

    /**
     * Returns the date of the user creation,
     * on a good and more readable format :)
     *
     * @return string
     */
    public function created_at()
    {
        return 'bla';
        // return String::date($this->created_at);
    }

    /**
     * Returns the date of the user last update,
     * on a good and more readable format :)
     *
     * @return string
     */
    public function updated_at()
    {
        return String::date($this->updated_at);
    }
}
HellPat commented 11 years ago

What if you var_dump your user in your template?

Shouldn't your presenter method be presentTest() to call $user->test (prefixed with "present") (I think this is optional)

HellPat commented 11 years ago

If your var_dump results in your user object you maybe forgot 'Illuminate\View\ViewServiceProvider' in your service providers?

newtonianb commented 11 years ago

Ah it works! I started again from scratch for the third time and this time i got it working, unfortunately i'm not 100% sure what I did different. Appreciate your help and if I figure out what I did different I'll comment.