英文:
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.
答案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;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论