Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers closure causes error in Laravel #365

Open
shummel-brunner opened this issue Aug 1, 2023 · 1 comment
Open

Helpers closure causes error in Laravel #365

shummel-brunner opened this issue Aug 1, 2023 · 1 comment

Comments

@shummel-brunner
Copy link

shummel-brunner commented Aug 1, 2023

The PHP Code:

   'helpers' => [
       'or' => function ($arguments) {
           $len = count($arguments) - 1;
           $options = $arguments[$len];
           $val = TRUE;

           for ($i = 0; $i < $len; $i++) {
               if (!$arguments[$i]) {
                   $val = FALSE;
                   break;
               }
           }

           return $val;
       },
       'not'=>function($val,$options){
           return !$val;
       },
       'eq'=>function($val1,$val2){
           return $val1==$val2;
       }
   ],

Laravel does not allow closures in config files (this particular one is in the handlebars.php in the config folder). It causes an error when you try run "php artisan cache:config":

$ sudo php artisan config:cache

   LogicException

Your configuration files are not serializable.

  at vendor/laravel/framework/src/Illuminate/Foundation/Console/ConfigCacheCommand.php:84
     80▕             require $configPath;
     81▕         } catch (Throwable $e) {
     82▕             $this->files->delete($configPath);
     83▕
  ➜  84▕             throw new LogicException('Your configuration files are not serializable.', 0, $e);
     85▕         }
     86▕
     87▕         $this->components->info('Configuration cached successfully.');
     88▕     }

  1   bootstrap/cache/config.php:1558
      Error::("Call to undefined method Closure::__set_state()")

      +14 vendor frames
  16  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

The real solution would be to add the "=" and "and" and "or" and "not" operators. Any language should have these. Short of that, there needs to be a way to put functions of some kind other than a closure in the config file, however you think is best so you can put custom functions somewhere. The ideal solution would be to fix both issues for maximum flexibility.

@shummel-brunner
Copy link
Author

This is on a Laravel 9 site. To be clear, more recent versions of Laravel do not allow closures in config files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant