【问题标题】:Can't create URL laravel 8无法创建 URL laravel 8
【发布时间】:2021-08-08 18:10:05
【问题描述】:

我正在学习 Laravel 8 的教程。我创建了一个包含 3 篇文章的简单视图。每篇文章都有一个指向另一个 html 页面的链接。练习是在web.php.ur 中创建 URL 我有 2 个视图 post.blade.php 和 posts.blade.php(主要的) ,html 中的其他文章包含在 ressources 的一个帖子文件夹中。

Route::get('posts/{post}', function ($view) {
    $post = file_get_contents(__DIR__ . "/../ressources/posts/{$view}.html");

    return view('post', [
        'post' => $post,
    ]);
});

这是网址:http://127.0.0.1:8000/posts/my-first-post

这是我得到的错误:

file_get_contents(C:\xampp\htdocs\laravel2\laravel2\routes/../ressources/posts/my-first-post.html):打开流失败:没有这样的文件或目录

我不明白出了什么问题,因为我正在复制教程正在做的事情。

【问题讨论】:

  • 本教程不适合学习Laravel。选择一个好的
  • ressources 很可能是resources。但是为什么你使用file_get_contents 而不是返回一个自动从资源文件夹中提取的view
  • 我想我只是要看另一个教程并重新开始谢谢大家

标签: php laravel laravel-8


【解决方案1】:

在 routes/web.php 页面中

Route::get('/posts', [PostController::class, 'index']);

在查看页面

 <a  href="{{url('/posts')}}">Posts</a>

【讨论】:

    【解决方案2】:

    首先,我想说的是,如果你的导师真的让你写这个 file_get_contents(__DIR__ . "/../ressources/posts/{$view}.html") ,那么他对 laravel 和 MVC 结构一无所知。别再追了。

    这是基于我从您的示例中理解的解决方案:

    1. 所以在“ressources/posts”目录中有一个名为“my-first-post.html”的 html 文件。将其重命名为“my-first-post.blade.php”
    2. 在 post.blade.php 文件中,只需使用 @include('posts.' . $post) 包含该文件
      并将控制器更改为:
        Route::get('posts/{post}', function ($post) {
            return view('post', [
                'post' => $post,
            ]);
        });
    

    【讨论】:

      【解决方案3】:

      从这里开始https://laracasts.com/series/laravel-8-from-scratch

      您将清楚地了解 laravel 的每个细节。这个系列非常精确地让你开始使用 laravel。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-20
        • 1970-01-01
        • 1970-01-01
        • 2021-08-18
        • 2018-05-06
        • 2018-03-06
        • 1970-01-01
        • 2021-09-28
        相关资源
        最近更新 更多