【问题标题】:Laravel Route::get() function and parameter confusingLaravel Route::get() 函数和参数混淆
【发布时间】:2014-03-22 11:05:56
【问题描述】:

大家好,我刚刚安装了 laravel4,花了两天时间尝试迈出第一步。现在我做到了,但我对 Route::get() 函数和他的参数感到困惑。

我是直接在里面安装laravel的

/opt/lampp/htdocs/laravel

然后按照教程创建文件

userform.php

进入app/views,然后将以下代码添加到routes.php中

Route::get('userform', function()
{
return View::make('userform');
});

。然后我去

/localhost/laravel/public

查看欢迎页面,以及

/localhost/laravel/public/userform

查看 view/userform.php 中定义的表单。

Q1:根据chrome dev tools,我在html页面看到,表单动作是

http://localhost/laravel/public/userform

但是在public下什么都没有

index.php, favicon.ico packages robots.txt

Q2:对于

Route::get('userform', function()
{
    return View::make('userform');
});

第一个“用户窗体”代表什么?根据官方教程,它应该是url,但前一部分是什么? 对于这一行

return View::make('userform')

我猜“userform”指的是文件/app/views/userform.php,对吧?

【问题讨论】:

    标签: laravel-4


    【解决方案1】:

    公共目录中的.htaccess 文件负责通过index.php 文件汇集所有传入请求。这允许 Laravel 获取 URI 并将其与您定义的路由匹配,并最终将您创建的视图返回给您。

    所以你请求 localhost/laravel/public/userform,请求通过 index.php 汇集,Laravel 被启动。 Laravel 挑选出 URI 的 userform 部分,并将其与您定义的路由匹配。它会找到您定义的路由并触发它并返回响应。

    您对第二个问题的想法也很清楚。当您调用View::make 时,第一个参数是您要“制作”的视图的名称。如果您将视图命名为 app/views/forms/user.php,那么您将在您的路线中返回它:

    return View::make('forms.user');
    

    或者你可以使用斜线:

    return View::make('forms/user');
    

    【讨论】:

    • 感谢您的回答。我从您的回答中获得了重要信息并对其进行了测试,并且我更好地理解了它。所以我只需要将“/localhost/laravel/public/ name ”与Route::get('name', function()) 匹配,仅此而已。
    猜你喜欢
    • 2021-01-07
    • 2020-03-12
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2021-06-23
    相关资源
    最近更新 更多