4 years ago

[L4] Using repository inside a filter method?

Posted 4 years ago by peanut

I'm confused on how to inject a repository inside a filter method so it can access certain methods within a repository. Therefore I have been keeping my filters outside the filters.php file that Laravel comes with. Right now, I have this:

<?php namespace Bunny\Controllers\Forum;

use Bunny\Controllers\BaseController;

class CategoryController extends BaseController {

    protected $dependencies = [
        'category' => 'Forum\IForumCategoryRepo'
    public function __construct()
        $this->beforeFilter('@canView', array('only' => array('getView')));
     * Check to see if user can post in this category
    public function canView( $route, $request )
        $cat = $this->category->getById( $route->parameter('two') );
        if( ! $cat->canBy( 'view' ) )
            return $this->redirect('insufficient-perms');

    public function getView( $slug, $id )
        $this->data['category'] = $this->category->getById( $id );
        $this->data['userRead'] = $this->read->getTopics();
        $this->data['topics'] = $this->topic->getPaginatedTopics( $id, $this->getPage() );
        $this->data['postsPerPage'] = $this->post->getPostsPerPage();

        return $this->render('forums.category', $this->data);


But then I would have this piece of code duplicated everywhere that wants to use the view filter.. to make sure it has that permission.. But I need to grab a different model at times that has permissions enabled on them.

Is there an easier way to go about doing this? I don't know if I'm overthinking it...

Please sign in or create an account to participate in this conversation.