Instantiate class within bootstrap/start.php file Laravel 4.2?

Asked

Viewed 89 times

1

I have an app running Laravel 4.2, I have a class that takes care of IP's in a blacklist, and this one is safe on the bench.

I need that when a user accesses my application the system validates if the IP is in the blacklist returns a 403, if you won’t let me in. I’m trying to instantiate the class BlackListIpController inside the archive bootstrap/start.php (file that starts the application) however, without success.

Code:

$model = new app\controllers\BlackListIPsController;
if($model::where('ip', $model->getIp())->first()) {
    abort(403);
}

Error:

[Fri Jun 9 09:19:18 2017] PHP Fatal error: Undefined Constant 'app controllers Blacklistipscontroller' in /home/user/projects/abc/bootstrap/start.php on line 16

[Fri Jun 9 09:36:53 2017] PHP Fatal error: Class 'app controllers Blacklistipscontroller' not found in /home/user/projects/abc/bootstrap/start.php on line 16

  • If you have to create a filter in the case of Laravel

  • 1

    Oh cool, can you give me a hand of how I do it ?

  • You can create a middleware to treat this.

  • 1

    @Kayobruno does not have middleware in 4.2 is filter!

  • @Danilotiagothaisantos made an example ... !!!

  • 1

    @Virgilionovic Vlws for warning bro, did not know this. I only worked with Laravel 5.x

Show 1 more comment

1 answer

1


Use the technique that is made for that which is Route Filters - Documentation, example:

Open the file app\filters.php and create a filter by name ipblock:

Route::filter('ipblock', function()
{
    // aqui você coloca a sua lógica se não passar só fiz uma 
    // cópia do que está na sua pergunta acho eu que está ainda
    // errado, mas, só corrigir a sua lógica.
    $model = new app\controllers\BlackListIPsController;
    if($model->where('ip', $model->getIp())->first()) {
        abort(403);
    }
});

after creating this filter you must enter the app\routes.php and create a Route::group with this filter, example:

Route::group(array('before' => 'ipblock'), function()
{
     // insira as rotas que seram verificadas e protegidas por
     // esse filtro, exemplo
     Route::get( ....

});

or if you want individually:

Route::get('user', array('before' => 'ipblock', function()
{
    return '...';
}));

ready if he does not pass the filter he 403 as you wish.

Observing:

To get the ip see link, example:

Request::getClientIp();

There is also a way to work with your application events or application filters that is by the commands (in your case it is App::before, but, added the others as example):

App::before(function($request)
{
    //coloque a lógica aqui
});

App::after(function($request, $response)
{
    //coloque a lógica aqui
});    

App::error(function(Exception $exception)
{
   //coloque a lógica aqui
});

which are also located in the app\filters.php.

References

  • Perfect guy ! I will use this, just a doubt: you created on top of specific routes, if I want it to be for every application, for example, as soon as it accesses my domain (when the application is instantiated) I already want to do the IP check. So for me actually no matter the route, the way it’s mounted I’ll have to put a group of routes with the new filter and all the routes of my application stay inside it... In this case, I need that as soon as the application is instantiated the system do this independent check of the route. will be that I can do this ?

  • 1

    @Danilotiagothaisantos yes I made the issue is the last part of the answer ..., but, still if you are going to give an opinion put this on the same routes ... but, of course there is the option ...

  • 1

    Man, congratulations, perfect ! thank you very much !!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.