izniburak / php-router

PHP Router, which also has rich features like Middlewares and Controllers is simple, useful and flexible router class for PHP.
https://github.com/izniburak/php-router/wiki
MIT License
236 stars 45 forks source link

cache #70

Open dvlpr1996 opened 2 years ago

dvlpr1996 commented 2 years ago

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'
];
izniburak commented 2 years ago

Hi, Could you please share your routes you set?

dvlpr1996 commented 2 years ago

@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();
izniburak commented 2 years ago

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.

dvlpr1996 commented 2 years ago

@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.

izniburak commented 2 years ago

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.