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

1

I have an application running Laravel 4.2 , I have a class that takes care of IP's in blacklist , and this is saved in the database.

I need when a user accesses my application the system validates if IP is in blacklist returns a 403 if it does not allow access. I'm trying to instantiate the BlackListIpController class inside the file bootstrap/start.php ( file that starts the application ), but 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

    
asked by anonymous 09.06.2017 / 14:38

1 answer

1

Use the technique that is done for this which is Route Filters , example:

Open the file app\filters.php and create a filter 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 should enter 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 to individually:

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

Ready if it does not pass in the same filter as 403 as you wish.

Note:

To get the ip see link , example:

Request::getClientIp();

There is also a way to work with the events of your application or filters the application that is by the commands (in your case it is App::before , but added others as an 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 app\filters.php .

09.06.2017 / 15:05