Reputation: 5064
I wonder if there is any performance issue with regards to dynamically loading routes via database.
In the routes.php
, it will retrieve values from the database and create the routing from there.
From the routes.php
:
buildDynamicRoutes(new App\Factory\ModelFactory('App\Models\RoutingModel'));
function buildDynamicRoutes($model)
{
$routes = $model->getInstance()->getRoutes(); // 'routes' is the table name wherein routes will be stored.
if (is_array($routes) && !empty($routes)) {
foreach ($routes as $route) {
Route::resource($route['url'], 'DynamicController');
}
}
}
So basically, on the buildDynamicRoutes
method, it will call the ModelFactory that instantiates the RoutingModel
class. Once instantiated, it will connect to the API thru the getRoutes()
method that will return the routes result.
From there, it will build the Route::resource
base on the $route['url']
returned which will use the DynamicController
.
Having this structure, will there be any performance issue with regards to routing or loading the web app? I'm asking this because I notice that my web app seems to load very slow, around 10-18 seconds page load.
I recently upgraded from Codeigniter to Laravel 5.2 and notice the drastic performance issue upon switching to Laravel. When I was previously using Codeigniter, the page loads in about 2-4 seconds only. Notice the huge difference it made.
Same logic, just re-write the code base on Laravel's standards.
Upvotes: 0
Views: 1451
Reputation: 823
Try this and let us know how it goes.
$routes = Cache::has('routes')
? Cache::get('routes')
: $model->getInstance()->getRoutes();
if (!Cache::has('routes')) {
Cache::add('routes', $routes, 120);
}
Replace with your "$routes = $model->getInstance()->getRoutes()" assignament. Maybe you need to import Cache:
use Illuminate\Support\Facades\Cache;
If your $routes values change frecuently you can set another cache time instead of 120 minutes.
Upvotes: 1