Search code examples
laravellaravel-bladelaravel-routingamazon-lightsail

Unable to load 45k root url in the laravel web.php


My client have 7896 pages on previous website URL of older website like: www.domain.com/{url}

Now client want to make new website but he need same url structure as older one (www.domain.com/{url})

Client have approx 45k blog post in different category & this need to be manage from laravel admin panel.

If i need to do that than i have 45k url in the root domain like (www.domain.com/{url}), here issue comes in that there are new development in the website also with route like

(www.domain.com/category/{cat_url}) (www.domain.com/tags/{cat_url})

I have 45k blog url in the web.php file

www.domain.com/{url}

Issue list

  • memory limit issue
  • slow performance issue (4.5 sec to load page, on each request load 45k url in the route)

now in the aws server i m facing issue related to memory timeout as i have large amount of URL in the root that is loaded into memory.

Error Message:

local.ERROR: Out of memory (allocated 38469632) (tried to allocate 3170560 bytes) {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Out of memory (allocated 38469632) (tried to allocate 3170560 bytes) at /domain.com/routes/dynamic_blog_routes.php:33026)
[stacktrace]
#0 {main}
"} 
  • I can not change URL structure as seo is already very well

Is there any way that we can solve this? Insted of 45k url in web.php to only 1 function or menthod we can use that?

Please share your solution or guide me on this.

My web.php file like.

<?php
Route::get('test-blog', 'Front\BlogController@blog_details');
Route::get('hello-world', 'Front\BlogController@blog_details');
--- 45k routes like this ---

Solution

  • @Vüsal Hüseynli

    Based on your comment i have solved this issue.

    My web.php file

    <?php
    Route::get('category/{any}', 'Front\BlogController@cat_details');
    Route::get('tags/{any}', 'Front\BlogController@tag_details');
    Route::get('{any}', 'Front\BlogController@blog_details');
    ?>
    

    My BlogController.php file

    class BlogController extends Controller
    {
        public function blog_details(Request $request)
        {
          $urlSegment = $request->segment(1);
          if(empty($urlSegment)){
              abort(404);
          }
          $blog = Blog::where("slug", "=", $urlSegment)->first();
          dd($blog);
        }
    }
    

    I can access all my blog routes as well as other urls that is not part of the blog.