Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vinoua's avatar

Unexpected Exception

Hello! Throws a 404 exception if more than one route parameter is used. I can't figure out what's wrong. Requested URI: www.site.loc/product-category/in-stock/page/2 If you request www.site.loc/product-category/in-stock/ everything works, but if you add page/2 , it throws a 404 exception. Route: Route::get('/product-category/{name}/{name1?}/{page?}',['uses'=>'ProductCategoryController@index']); Controller part: public function index( $name, $name1 = null, $page = null ) { $category = DB::table('categories')->where('alias', $name)->first(); dd($category); if ( empty( $category ) ) { abort( 404 ); } If you request with one parameter, it prints out the contents of $category, and otherwise - a 404 exception. I tried to make all parameters mandatory and vice versa - it still works with one, but if more than one does not. Why is that?

0 likes
27 replies
Akash_kushwaha's avatar

Hello @vinoua can please use the code and show me the values you are getting.

	public function index( $name, $name1 = null, $page = null ) {
 		$category = DB::table('categories')->where('alias', $name)->first();
		// dd($category); 
		 echo "<pre>";
		print_r($name);
 		print_r($name1);
		print_r($page);
		die;
		 if ( empty( $category ) ) { 
				abort( 404 ); 
		}
	}
vinoua's avatar

@Akash_kushwaha Wrote the code as you said. The same custom 404 page of mine is displayed. Sorry, I can't paste the link to the image since I'm on my first day here. Here is my code from Exceptions/Handler.php (In my opinion, everything is correct here) public function render($request, Exception $e) { if ( $this->isHttpException( $e ) ) { $statusCode = $e->getStatusCode();

        switch ( $statusCode ) {
            case '404' :
                $obj = new \Adamoto\Http\Controllers\SiteController( new \Adamoto\Repositories\MenusRepository( new \Adamoto\Menu ) );
                $menus = $obj->getMenu();
                $main_nav = view( env('THEME').'.main_nav' )->with( 'menus', $menus )->render();
                $main_mnav = view( env('THEME').'.main_mnav' )->with( 'menus', $menus )->render();
                $foot1_nav = view( env('THEME').'.footer_nav' )->with( 'menus', $menus )->render();
                $foot2_nav = view( env('THEME').'.footer_nav2' )->with( 'menus', $menus )->render();
                $breadcrumbs = [ 
                    0   =>  [ 
                        'name'  =>  'Головна',
                        'url'   =>  route('home')
                    ],
                    1   =>  [ 
                        'name'  =>  'current',
                        'url'   =>  'Помилка 404: Сторінку не знайдено'
                        ]
                    ];
                $title = 'Не знайдено';
                $breadcrumbs = view( env('THEME').'.breadcrumbs' )->with( 'breadcrumbs', $breadcrumbs )->render();
                $title = view( env('THEME').'.title' )->with( 'title', $title )->render();
                
                \Log::alert('Страница не найдена : ' . $request->url() );
                
                return response()->view( env('THEME') . '.404', [
                    'main_nav'      =>  $main_nav,
                    'main_mnav'     =>  $main_mnav,
                    'footer_nav'    =>  $foot1_nav,
                    'footer_nav2'   =>  $foot2_nav,
                    'titleM'        =>  'Не знайдено',
                    'breadcrumbs'   =>  $breadcrumbs,
                    'description'   =>  'Помилка 404: Сторінку не знайдено'
                ] );
        }
    }

    return parent::render($request, $e);
}
Sinnbeck's avatar

@vinoua never use env outside of config. Move it to a config file and call it from there

Sinnbeck's avatar

My bet is that you have an earlier route that you hit. Try moving the route to the top of the web.php file

1 like
vinoua's avatar

@Sinnbeck Here is my routes.php . And you're right, I used env outside of the config. Now I'll try to change it.

Route::resource('/', 'IndexController', ['only'  =>  ['index'], 'names' =>  ['index' =>  'home']]);
Route::get('/blog/archives/{year}/{month}/{page?}',['uses'=>'BlogArchivesController@index']);
Route::get('/blog/{page?}',['uses'=>'BlogController@index']);
Route::get('/product-category/{page1}/{page2}/{page3}',['uses' => 'ProductCategoryController@index']);
Route::post('/product-category/filter', ['uses' => 'ProductCategoryController@filter']);
Route::get('/product/{name}',['uses'=>'ProductController@index']);
Route::post('/order/create/{id}/{is_basic}', ['uses' => 'OrderController@create', 'as' => 'orderCreate']);
Route::group(['middleware' => 'web'], function() {
    Route::auth();
});
Route::group(['prefix' => 'account', 'middleware' => 'auth'], function() {
    Route::get('/',['uses'=>'Account\IndexController@index','as'=>'accountIndex']);
});
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
    Route::get('/',['uses'=>'Admin\IndexController@index','as'=>'adminIndex']);
    Route::resource('/products','Admin\ProductsController');
    Route::resource('/auction-products','Admin\AucProductsController');
    Route::resource('/menus','Admin\MenusController');
    Route::resource('/orders','Admin\OrdersController');
    Route::resource('/pages','Admin\PagesController');
    Route::resource('/posts','Admin\PostsController');
    Route::resource('/users','Admin\UsersController');
    Route::resource('/categories', 'Admin\CategoriesController');
    Route::get('/media-library', 'Admin\MediaLibraryController@index');
    Route::resource('/tools', 'Admin\ToolsController');
    Route::resource('/options', 'Admin\OptionsController');
    Route::post('/posts/change_limit', [ 'uses' => 'Admin\PostsController@change_limit', 'as' => 'adminPostsChangeLimit' ] );
    Route::post('/posts/search', [ 'uses' => 'Admin\PostsController@search', 'as' => 'adminPostsSearch' ] );
    Route::post('/pages/change_limit', [ 'uses' => 'Admin\PagesController@change_limit', 'as' => 'adminPagesChangeLimit' ] );
    Route::post('/pages/search', [ 'uses' => 'Admin\PagesController@search', 'as' => 'adminPagesSearch' ] );
    Route::post('/categories/change_limit', [ 'uses' => 'Admin\CategoriesController@change_limit', 'as' => 'adminCategoriesChangeLimit' ] );
    Route::post('/categories/search', [ 'uses' => 'Admin\CategoriesController@search', 'as' => 'adminCategoriesSearch' ] );
    Route::post('/products/change_limit', [ 'uses' => 'Admin\ProductsController@change_limit', 'as' => 'adminProductsChangeLimit' ] );
    Route::post('/products/search', [ 'uses' => 'Admin\ProductsController@search', 'as' => 'adminProductsSearch' ] );
    Route::post('/auction-products/change_limit', [ 'uses' => 'Admin\AucProductsController@change_limit', 'as' => 'adminAucProductsChangeLimit' ] );
    Route::post('/auction-products/search', [ 'uses' => 'Admin\AucProductsController@search', 'as' => 'adminAucProductsSearch' ] );
    Route::post('/orders/change_limit', [ 'uses' => 'Admin\OrdersController@change_limit', 'as' => 'adminOrdersChangeLimit' ] );
    Route::post('/orders/search', [ 'uses' => 'Admin\OrdersController@search', 'as' => 'adminOrdersSearch' ] );
    Route::post('/users/change_limit', [ 'uses' => 'Admin\UsersController@change_limit', 'as' => 'adminUsersChangeLimit' ] );
    Route::post('/users/search', [ 'uses' => 'Admin\UsersController@search', 'as' => 'adminUsersSearch' ] );
    Route::post('/options/change_limit', [ 'uses' => 'Admin\OptionsController@change_limit', 'as' => 'adminOptionsChangeLimit' ] );
    Route::post('/options/search', [ 'uses' => 'Admin\OptionsController@search', 'as' => 'adminOptionsSearch' ] );
    Route::post('/tools/change_limit', [ 'uses' => 'Admin\ToolsController@change_limit', 'as' => 'adminToolsChangeLimit' ] );
    Route::post('/tools/search', [ 'uses' => 'Admin\ToolsController@search', 'as' => 'adminToolsSearch' ] );
    Route::post('/uploads', [ 'uses' => 'Admin\UploadsController@index', 'as' => 'adminUploads' ] );
    Route::post('/uploaded-list', [ 'uses' => 'Admin\UploadedListController@index', 'as' => 'adminUploadedList' ] );
    Route::post('/uploaded-list-folders', [ 'uses' => 'Admin\UploadedListController@folders', 'as' => 'adminUploadedListFolders' ] );
    Route::post('/uploaded-list-search', [ 'uses' => 'Admin\UploadedListController@search', 'as' => 'adminUploadedListSearch' ] );
});
Route::get('/{page1}/{page2?}/{page3?}/{page4?}/{page5?}',['uses'=>'PageController@index']);
Sinnbeck's avatar

@vinoua please format your code by adding ``` on the line before and after it

vinoua's avatar

@Sinnbeck I changed all occurrences of env('THEME') in the files for this route to config('settings.theme') and also moved the route up first. But still throwing a 404 exception.

Sinnbeck's avatar

@vinoua another thing you can try is to set the route to accept everything and extract it in the controller

Route::get('/product-category/{slashData?}', 'ProductCategoryController@index')
    ->where('slashData', '(.*)');

And in the controller

public function index($slashData)
{
     $params = explode('/', $slashData);
    $cat = $params[0];
    $name = $params[1] ?? null;
    $name2 = $params[2] ?? null;

} 
1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@vinoua it still gives 404? Fixed typo

Are your routes cached?

php artisan route:clear 
1 like
Sinnbeck's avatar

@vinoua sadly I don't know remember much from back then as I have updated all of my own projects to laravel 8 or 9

Have you considered updating? It's hard when we can't recreate the problem

vinoua's avatar

@Sinnbeck Yes! Earned! When I tried to solve the problem myself - I cleared the routes cache, and then forgot about it) I'm sorry! Thanks a lot!

vinoua's avatar

@Sinnbeck The fact is that I am from Ukraine and studied at free lessons in 2016, and the project needs to be done quickly. Will definitely upgrade to 8 or 9.

Sinnbeck's avatar

@vinoua totally fair. Awesome that it's now working :)

Not quite sure what you meant by

So it can work just with data types?

And hope you are well and in safety.

1 like
vinoua's avatar

@Sinnbeck Yes, I worded it wrong. I meant a variant which you offered before slashData. But I checked and it doesn't work even with clearing the cache. As we say - All's well that ends well :) Thank you!

Sinnbeck's avatar

@vinoua written from memory so maybe there is a typo. But yeah you can have any route by telling the route with regex how it should be

1 like
Akash_kushwaha's avatar

@vinoua you are using so many same route with different parameters. because Routing follows top to bottom approach. It will execute the first route. so try changing your routes like .

	Route::get('/product-category/{name}/pages/{page?}',['uses'=>'ProductCategoryController@index']);
	Route::get('/product-category/{page1}/{page2}/{page3}',['uses' => 'ProductCategoryController@index']);
	Route::post('/product-category/filter', ['uses' => 'ProductCategoryController@filter']);

Change them to something like this.

	Route::post('/product-category/filter', ['uses' => 'ProductCategoryController@filter']);
	Route::get('/product-category/in-stock/pages/{page?}',['uses'=>'ProductCategoryController@index']);
	Route::get('/product-category/something-different-here/{page2}/{page3}',['uses' => 'ProductCategoryController@index']);

It wil make your route unique and word accordingly.

vinoua's avatar

@Akash_kushwaha I'm migrating a site from WordPress to Laravel 5.2 and I don't want to change the scheme of indexed routes. I have created one laravel route for categories, the first parameter is the category alias, the second optional is the intermediate word 'page', the third is the page number. I tried different options for requiring the parameters of this route and in a hurry showed the wrong route. I can't make multiple routes for each category because I need an alias as a parameter. Here is the route I need:

Route::get('/product-category/{name}/{name2?}/{page3?}',['uses' => 'ProductCategoryController@index']);
jdc1898's avatar

php artisan route:list | grep product-category/in-stock/page

Please or to participate in this conversation.