I had some (main) performance problems in the project that I am working on, and after registering all the requests that are being executed, I realized that many of them are being executed several times, and I cannot get to the root of the problem.
All requests that are executed multiple times belong to my composer supplier.
This is what my composer looks like:
public function boot() { view()->composer('partials.sidebar', function ($view) { $currentCategory = $this->getCurrentCategory(); $sidebarCategories = SidebarCategory::category($currentCategory) ->order() ->get(); $view ->with('sidebarCategories', $sidebarCategories); }); view()->composer('partials.footer', function ($view) { $footerLinks = FooterCategory::with('links.translations')->order()->get(); $footerColumn1 = $footerLinks->filter(function ($value, $key) { return $value->column == 1; }); $footerColumn2 = $footerLinks->filter(function ($value, $key) { return $value->column == 2; }); $footerColumn3 = $footerLinks->filter(function ($value, $key) { return $value->column == 3; }); $footerColumn4 = $footerLinks->filter(function ($value, $key) { return $value->column == 4; }); $view ->with(compact('footerColumn1', 'footerColumn2', 'footerColumn3', 'footerColumn4')); }); }
Both of these queries (Sidbar and Footer categories) are executed approximately 6 times, although each of them is called exactly once. Both of them are invoked in the main view using @include ('partialname').
I tried this:
if($view->offsetExists('sidebarCategory')) return;
But offsetExists always returns false (even after calling it for 5. time).
Any idea why this is happening and what am I doing wrong?
Edit:
I understood where the problem is. On the page I visit (when these few requests are executed), there are about 404 elements (images mostly). Every time a file is not found, a new 404 exception is thrown. And every time a 404 exception is thrown, a 404 scan => is done the same as the footer / sidebar requests (since they are part of the 404 view). Example: http://imgur.com/a/RrmOD
So, the question is how to prevent the visualization of the view when it is not necessary (for example, 404 is an image that was not found).
Here is a snippet of code from my routes, which I believe is the reason this happens:
Route::get('{slug}', ['as' => 'findBySlug', 'uses' => function($slug) { if(\App\DynamicCategory::findBySlug($slug)->count() > 0) return App::make('App\Http\Controllers\GeneralController')->getDynamicCategoryIndex($slug); else if(\App\DynamicPage::noCategory()->findBySlug($slug)->count() > 0) return App::make('App\Http\Controllers\GeneralController')->getDynamicPage($slug); else abort(404); }]);
PS: I know that this piece of code is extremely non-optimized (since it basically executes the same query twice, once, to see if the element exists, at another time, actually in the controller). This is a work in progress, and it is on the task list.
Edit 2:
I came up with the following solution, I am open to improvements, as this is a somewhat hacky way (if I add more folders, I will also need to remember this). I have only 3 direct subfolders in the shared folder: something, files and resources.
The solution is to check the first segment of the URL when rendering the exception (in the app / Exceptions / Handler.php file) and return a 404 response without presentation if it matches one of three folders:
public function render($request, Exception $e) { $firstSegment = $request->segment(1); if(starts_with($firstSegment, 'files') || starts_with($firstSegment, 'something') || starts_with($firstSegment, 'resources')) { return response('Stran ne obstaja', 404); } return parent::render($request, $e); }
Thanks in advance