2

How can I pass a variable that is processed in the filter back to route for later usage?

Example:

Filter:

Route::filter('myfilter', function()
{
    return "some data";
});

Route:

Route::get('/mypage', array('before'=>'myfilter', function($filter) {
    if($filter!= 'admin') {
        return Redirect::to('home');
    }
}));

The above example doesn't work. How can I make it works?

Thank you.

2
  • If you wanna check if someone is an admin, its better to do that in the filter. Commented Jan 8, 2014 at 8:59
  • Have you considered using the Session class? Commented Jan 8, 2014 at 10:16

4 Answers 4

1

WARNING: THIS DOESN'T SEEM TO WORK!


Store it in the IoC container: http://laravel-recipes.com/recipes/3

Filter:

Route::filter('myfilter', function()
{
    App::instance('app.name', 'John Doe'); // store var
});

Route:

Route::get('/mypage', array('before'=>'myfilter', function() {
    $name = app('app.name'); // read var
    return $name;
}));
Sign up to request clarification or add additional context in comments.

1 Comment

Tried with your example, it doesn't work. the App::instance doesn't pass to route. If try to access it in filter, it's fine. But on route, it throw an error saying "Class app.name does not exist"
0

You cannot. If you return anything from filters Laravel will return that filter to your browser and end your request.

Whay you can do is to redirect to a route and pass values to that route:

Route::filter('myfilter', function()
{
    return Redirect::to('mynewpage')->with('var', 'some data');
});

This data will come back in the Session:

$data = Session::get('var');

1 Comment

It is possible, see @chris342423's answer. While it's true if you try and return something from a filter it will end the request, that's not the only way to pass data back to the route.
0

You may pass data by any of below methods:

  • Session::put() and Session::get() ( slow because of disk writes )
  • Config::set() and Config::set() Defining PHP constant by
  • define() ( recommended for your purpose ) Setting and getting a
  • global variable by $_GLOBALS[] array ( not recommended )

Comments

0

Recently working in an old Laravel 4.2 project & approached this with a controller method as Controller Filter:

namespace App\Controllers;

Class MyController extends BaseController
{
    protected $filterValue;

    public function __construct()
    {
        $this->beforeFilter('@myFilter', array(
            'only' => 'myPage'
        ));
    }

    public function myFilter($route, $request)
    {
        $this->filterValue = 'some data';
    }

    public function myPage()
    {
        if ($this->filterValue != 'admin') {
            return Redirect::to('home');
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.