fideloper / ResourceCache

Handle Validation Cacheing and Concurrency Control (Etags/Modified Dates) in Laravel 4
7 stars 1 forks source link

Can't setup with Laravel 4, Docs not clear. #1

Closed JonathanHindi closed 11 years ago

JonathanHindi commented 11 years ago

I am trying to set the package on a Laravel 4 installation, The docs aren't clear about what to do for laravel and what to do for other implementations.

Model:

use LaravelBook\Ardent\Ardent;
use Fideloper\ResourceCache\Resource\ResourceInterface;

class Category extends Ardent implements ResourceInterface{

Controller Action:

/**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($categoryId)
    {
        // Get Catgeory
        $category = Category::with('image', 'products')
                        ->find($categoryId);

        // If result not empty
        if( $category )
        {
            if( ! ResourceRequest::wasModified($category) )
            {
                return Response::json(null, 304);
            }

            return ResourceResponse::asJson($category);
        }

        return App::abort(404);
    }

When running the action I got this error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class Category contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Fideloper\ResourceCache\Resource\ResourceInterface::getEtag, Fideloper\ResourceCache\Resource\ResourceInterface::getLastUpdated)

I think I am missing something in the implementation.

fideloper commented 11 years ago

Howdy -

It looks like your class Category is not be defining the methods as required by the ResourceInterface, which requires the declaration of a getEtag() method and getLastUpdated() method in any implementing class.

I've implemented ResourceInterface for the Eloquent ORM in this library - However, since you are using Ardent, you will need to create your own implementation of getEtag() and getLastUpdated() that work with the Ardent.

Note: - I plan on rewriting this library almost completely, as the methods wasModified and wasNotModified don't really solve the problem domain in as elegant a fashion as it could (In other words, this code still needs work :D ).

I may also move the generation of ETags to a separate "service" classes, which would remove the need for an interface and an implementation (which in the case of my code means eliminating the need for extending Eloquent).

That being said, this libraries current incarnation is perfectly acceptable for Conditional GET's (wasModified) and Concurrency Control (wasNotModified()). There's just some more leg-work on your end to make your Ardent model implement the ResourceInterface class.

Hope that makes senes!

JonathanHindi commented 11 years ago

Since you are planning to rewrite it, I will hold on using the package for now and implemented later when you update the package. Thanks Anyway :),

fideloper commented 11 years ago

Hello I have a new library which does the client-side of things. That is up here: https://github.com/fideloper/ConditionalRequest with some Laravel-based explanation here: https://github.com/fideloper/ConditionalRequest/wiki/Laravel-Symfony

Note: This doesn't generate ETags for you - this is purely the request side, and doesn't attempt to use Eloquent (etc) - That'll be a separate package.