Determine route according to the url that called the request

Asked

Viewed 121 times

0

My situation is as follows, I am using a form of charging that makes the entire online subscription system, the system when performing the registration of a customer and validate the payment of the same, frees access for 30 days...

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

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

But when giving the refresh on the page it no longer enters the middleware Validade...

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,
    ];
}

On my routes I have a Route::group which contains all routes that are validated by this middleware along with middleware web, 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 whether there is any way I can get 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 could be clear enough, I appreciate the help.

1 answer

0


I solved it in a very simple way:

I took responsibility for rerouting the routes and I played this on my controller, in short my middleware was 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 sending here and played inside 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
}

Browser other questions tagged

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