php-router icon indicating copy to clipboard operation
php-router copied to clipboard

cache

Open dvlpr1996 opened this issue 3 years ago • 5 comments

Hi, How can we set cache param? This is how I enter it in the config array and get error. Warning: foreach() argument must be of type array|object, int given in c:\xampp\htdocs....\ vendor\izniburak\router\src\Router.php on line 271

[
	'base_folder' => 'public/',
	'main_method' => 'index',
	'paths' => [
		'controllers' =>  'app/controllers',
		'middlewares' =>  'app/middlewares'
	],
	'namespaces' => [
		'controllers' => 'app\controllers',
		'middlewares' => 'app\middlewares'
	],
	'debug' => true,
	'cache' => 'cache/route/cache.php'
];

dvlpr1996 avatar Sep 18 '22 18:09 dvlpr1996

Hi, Could you please share your routes you set?

izniburak avatar Sep 18 '22 21:09 izniburak

@izniburak

use Buki\Router\Router;
use app\controllers\HomeController;
use app\controllers\CharacterController;

$routes = new Router(
[
	'base_folder' => 'public/',
	'main_method' => 'index',
	'paths' => [
		'controllers' =>  'app/controllers',
		'middlewares' =>  'app/middlewares'
	],
	'namespaces' => [
		'controllers' => 'app\controllers',
		'middlewares' => 'app\middlewares'
	],
	'debug' => true,
	'cache' => 'cache/route/cache.php'
]);

$routes->get('/', [HomeController::class, 'index'],['name' => 'home.index']);
$routes->get('/character/:id', [CharacterController::class, 'index'],['name' => 'character.index']);

$routes->run();

dvlpr1996 avatar Sep 19 '22 03:09 dvlpr1996

Did you use cache method for storing your routes? Example:

$routes->cache();

I tried similar scenario, but I couldn't get any error. It seems that everything's okay.

izniburak avatar Sep 21 '22 06:09 izniburak

@izniburak

After using $routes->cache(), it still gives the same error Uncaught TypeError: Buki\Router\Router::getRoutes(): Return value must be of type array, int in izniburak\router\src\Router.php on line 470

it return int 1 in loadCache() method line on line 694 ($this->routes = require_once $this->cacheFile;)

But when I use Cache() method (copy and past method body) outside Buki\Router\Router class , the cache file fills up.

dvlpr1996 avatar Sep 22 '22 13:09 dvlpr1996

Could you delete your cache file and try again to cache your routes again? Probably, you have an issue about cached routes file. After the loading cached routes process, it returns a wrong value because of the old cached file.

izniburak avatar Sep 24 '22 20:09 izniburak