0
votes

I'm having this issue in laravel 8 where the new routes I'm adding give me a 404 right away, but when I di PHP artisan route: list they are shown: here my routes

// Rutas de vistas públicas
Route::group(['prefix' => '/mx/capitulos'], function(){

    // vista publica que regresa cuando es autenticada desde link dinamico
    Route::any('/vp/{id_user}', function ($id_user) {

        $user = Cargo::where('id_usuario', '=', $id_user)->orderBy('id_puesto', 'asc')->first();
        $capitulo = Capitulo::where('id_capitulo', '=', $user->id_capitulo)->first();

        if($capitulo == null){
            abort(401);
        }

        return view('vp_main')->with([
            'id_capitulo' => $capitulo->id_capitulo,
            'nombre_capitulo' => $capitulo->nombre_capitulo,
            'id_usuario' => $user->id_usuario,
        ]);

    })->name('vp_usuarios');

    // rutas para autenticacion de link dinamico
    Route::any('/auth/{url_token}/{id_user}', function (Request $request, $url_token, $id_user) {

        $token_admin = new TokenController;
        return $token_admin->abrirVentanillaPagos($request, $url_token, $id_user);

    })->name('vp_entrada');

    // vista que busca el nombre del capitulo escrito en el navegador
    Route::get('/{nombre_cap?}/{id_usuario?}', function ($nombre_cap = null) {

        $capitulo = Capitulo::where('nombre_capitulo', 'like', '%'. $nombre_cap .'%')->first();

        if($capitulo == null && $nombre_cap == 'ventanilla_pagos'){

            $nombre_capitulo = "Ventanilla de Pago - Capitulos";
            return view('vp_main')->with('nombre_capitulo', $nombre_capitulo);

        } elseif($capitulo !== null ) {

            return view('vp_main')->with([
                'id_capitulo' => $capitulo->id_capitulo,
                'nombre_capitulo' => $capitulo->nombre_capitulo,
            ]);

        } else {
            abort(404);
        }
    })->name('vp_capitulo');

    // redireccion a la segunda vista para efectuar el pago
    Route::any('/vp/completar_pago/{id_capitulo}', function ($id_capitulo) {

        return view('vp_completar')->with(['id_capitulo' => $id_capitulo]);

    })->name('vp_completar');

});

and my route:list result

| GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | mx/capitulos/auth/{url_token}/{id_user}      | vp_entrada            | Closure
                                                   | web                                             |
|        | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | mx/capitulos/vp/completar_pago/{id_capitulo} | vp_completar          | Closure
                                                   | web                                             |
|        | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | mx/capitulos/vp/{id_user}                    | vp_usuarios           | Closure
                                                   | web                                             |
|        | GET|HEAD                               | mx/capitulos/{nombre_cap?}/{id_usuario?}     | vp_capitulo           | Closure
                                                   | web                                             |

| |

The rest of the routes are working fine, just those that I have listed are the ones throwing the 404 pages. There are no logs available. Any help would be very appreciated. Thanks in advance

2
use controllers to write functionSupunSpera
show your url , do u enter the url correctly ? (clear cache if u have no any mistake)Abbas
full URL is : code.regionadmin.com/mx/capitulos/ventanilla_pagos. I have cleared cache and optimize:clear but now it erased all my sass styles :(Nyalum Lacey
Those functions are there because they check whether a user enters a team name and to redirect it according to thatNyalum Lacey
Your URL does not have a route in the route:list result.John Hanley

2 Answers

1
votes
1
votes

the problem i see here is probably with this route

//any two parameter will match these parameter and it will execute.
 //the number of parameter does not matter
     Route::get('/{nombre_cap?}/{id_usuario?}', function ($nombre_cap = null) {
    
            $capitulo = Capitulo::where('nombre_capitulo', 'like', '%'. $nombre_cap .'%')->first();
    
            if($capitulo == null && $nombre_cap == 'ventanilla_pagos'){
    
                $nombre_capitulo = "Ventanilla de Pago - Capitulos";
                return view('vp_main')->with('nombre_capitulo', $nombre_capitulo);
    
            } elseif($capitulo !== null ) {
    
                return view('vp_main')->with([
                    'id_capitulo' => $capitulo->id_capitulo,
                    'nombre_capitulo' => $capitulo->nombre_capitulo,
                ]);
    
            } else {
                abort(404);
            }
        })->name('vp_capitulo');

There are two optional parameter in the route and no compulsory parameter so any thing that comes will match to this route and this will execute the route and the route will never search for next route since it has a matched parameter.

for example... when you call this route it will still execute the above route since it matches to the above route which could be causing the 404 error since it could not find the result.

//vp becomes first parameter for {nombre_cap?} and completar_pago will become the second parameter for {id_usuario?}.
//it will never look for third parameter once it has found its match

    Route::any('/vp/completar_pago/{id_capitulo}', function ($id_capitulo) {
        
                return view('vp_completar')->with(['id_capitulo' => $id_capitulo]);
        
            })->name('vp_completar');

and similar will happen with any new route that you add in the given prefix group. One thing you can do is move the above route with optional parameters to the last since this route will execute only when no other route match the parameter.