Determine route according to the url that called the request

0

My situation is as follows, I am using a form of billing that makes the whole system of subscription online, the system when registering a customer and validating the payment of the same, frees the access for 30 days ...

But at the end of this time I will have to do a form of "blocking" the system, I have already done everything I need, but I have reached a code that satisfies what I need.

I created a middleware that helps me to receive and validate the clients that have the expired plan, the result for any route that I access is this (which is the expected result):

Butwhenyourefreshthepage,itdoesnotenterthemiddlewareValidade...

  

Validity.php

<?php namespace App\Http\Middleware; use App\Empresa as Empresa; use Closure; use Auth; class Validade{ public function handle($request, Closure $next){ $empresa = Empresa::where('id', Auth::user()->idglobal)->first(); $databx = new \DateTime(); $datavc = new \DateTime($empresa->permissao_uso); $diasatraso = ($databx->diff($datavc)->format('%r%a') < 0) ? $databx->diff($datavc)->format('%a') : 0; if($diasatraso > 0){ return redirect('lanc')->withErrors(['Plano Expirado' => '[Plano Expirado!] - Adquira um novo pacote de serviços para continuar usufruindo de nossos serviços!']); }else{ return $next($request); } } }
  

kernel

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [

            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'validade' => \App\Http\Middleware\Validade::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

In my routes I have a Route::group that contains all the routes that are validated by this middleware along with the web middleware, is working perfectly:

  

web.php

<?php
use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//rotas de autenticação
Auth::routes();

//Aqui está meu problema
route::resource('lanc', 'LancamentoController')->middleware('auth');

Route::group(['middleware' => 'auth', 'middleware' => 'validade'], function()
{
    //produtos
    Route::resource('produtos','ProdutoController');

    //restante das rotas ocultas...
}

In case all my routes are validated ... my question is if there is any way for me to tell the routes where the request is coming from, select the middleware and make the handler of the request in question. .

I do not know if I can be clear enough, thanks for the help.

    
asked by anonymous 22.11.2018 / 18:21

1 answer

0

I solved it in a very simple way:

I took responsibility for the redirection of the routes and threw this in my controller, in summary my middleware looks like this:

  

Validity.php

<?php

namespace App\Http\Middleware;

use App\Empresa as Empresa;
use Closure;
use Auth;

class Validade{
    public function handle($request, Closure $next){
        $empresa = Empresa::where('id', Auth::user()->idglobal)->first();

        $databx = new \DateTime();
        $datavc = new \DateTime($empresa->permissao_uso); 

        $diasatraso = ($databx->diff($datavc)->format('%r%a') < 0) ? $databx->diff($datavc)->format('%a') : 0;

        if($diasatraso > 0){
            return redirect('lanc');
        }else{
            return $next($request);
        }   
    }
}

I removed the error here and played within the index function in my controller:

  

LancamentoController.php

<?php

//cabeçalho oculto...

class LancamentoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $lanc = Lancamento::where('idglobal', Auth::user()->idglobal)->get();

        $empresa = Empresa::where('id', Auth::user()->idglobal)->first();

        $databx = new \DateTime();
        $datavc = new \DateTime($empresa->permissao_uso); 

        $diasatraso = ($databx->diff($datavc)->format('%r%a') < 0) ? $databx->diff($datavc)->format('%a') : 0;

        if($diasatraso > 0){
            return view('Financeiro.resumo', ['lancamentos'=> $lanc])->withErrors(['Plano Expirado' => '[Plano Expirado!] - Adquira um novo pacote de serviços para continuar usufruindo de nossos serviços!']);
        }else{
            return view('Financeiro.resumo', ['lancamentos'=> $lanc]);
        }

    }

//Código oculto
}
    
22.11.2018 / 18:48