Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johninthout's avatar

Using a filter in a package

Since Laravel 4.3 is coming I started creating a package in the Dev branch of laravel. Now everything works like a charm the only thing I bumb in to is that I need to use a Filter defined in my Package folder to check the filter in my controller. but it can't find it.

VisitorFilter.php

<?php namespace Woodmarks\Mila\Http\Filters;

use Auth, Redirect;

class VisitorFilter {

    /**
     * Run the request filter.
     *
     * @return mixed
     */
    public function filter()
    {
        if (Auth::check())
        {
            return Redirect::to('Woodmarks\Mila\Http\Controllers\Auth\AuthController@getLogin');
        }
    }

}

And in my controller AuthController.php

public function __construct (Authenticator $auth) {
    $this->auth = $auth;
    $this->beforeFilter('csrf', ['on' => ['post']]);
    $this->beforeFilter('visitor', ['except' => ['getLogout']]);
}

But it can't find the visitor filter. Should i register the filter in my service provider or something? Or should i add a namespace to the beforeFilter?

0 likes
5 replies
kreitje's avatar

You will need to stick it in the FilterServiceProvider.

protected $filters = [
  'auth' => 'App\Http\Filters\AuthFilter',
  'auth.basic' => 'App\Http\Filters\BasicAuthFilter',
  'csrf' => 'App\Http\Filters\CsrfFilter',
  'guest' => 'App\Http\Filters\GuestFilter',
  'visitor' => 'Woodmarks\Mila\Http\Filters\VisitorFilter',
 ];
johninthout's avatar

@kreitje thanks for the reply! I see but it is a package that should not depend on a fresh installation and work is there a good way to put it in my own service provider by calling the ioc container?

johninthout's avatar

Ah right awesome! Thanks for your help! will work it out with that!

Please or to participate in this conversation.