在Laravel的`routes/web.php`文件中未定义的全局变量。

huangapple go评论65阅读模式
英文:

undefined global variable in routes/web.php laravel

问题

I am trying to define a global variable to use in multiple Laravel routes. However, I am getting "undefined variable $title" as an error message. What am I doing wrong? The code at the bottom throws a similar error.

$title = "YAAA";
Route::get('/insertORM', function(){
    $a = $title;
});

这段代码存在于标准的 Laravel 应用程序中,位于 routes/web.php 文件中。

英文:

I am trying to define a global variable to use in multiple Laravel routes. However, I am getting "undefined variable $title" as error message. What am I doing wrong? The code on the bottom, throws a similar error.

$title = "YAAA";
Route::get('/insertORM', function(){
    $a = $title;
});

This piece of code exists in a standard laravel application inside the routes/web.php file.

在Laravel的`routes/web.php`文件中未定义的全局变量。

答案1

得分: 1

The function needs to inherit the variable from the parent scope by using use()

Route::get('/insertORM', function() use ($title, $body) {
    $a = $title;
    $b = $body;
});
英文:

The function needs to inherit the variable from the parent scope by using use()

Route::get('/insertORM', function() use ($title, $body) {
    $a = $title;
    $b = $body;
});

huangapple
  • 本文由 发表于 2023年2月14日 04:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定