【问题标题】:How to access url variable using Slim3 and Eloquent ORM?如何使用 Slim3 和 Eloquent ORM 访问 url 变量?
【发布时间】:2018-03-18 11:33:08
【问题描述】:

我正在尝试掌握使用 Eloquent ORM 的窍门,因此我正在使用 Slim3/Twig 从头开始​​创建一个简单的博客。我已经成功地创建了一个链接到两个表(用户表、帖子表)的用户模型和帖子模型,并且能够将博客帖子链接动态插入到我的“主页”树枝模板中。

namespace App\Controllers;

use App\Models\User;
use App\Models\Post;
use Slim\Views\Twig as View;
use Illuminate\Database\Eloquent\Model;

class StoryListController extends Controller
{
  public function index($request, $response)
  {
    $posts = Post::join('users', 'posts.user_id', '=', 'users.id')->get();

    return $this->container->view->render($response, 'home.twig', [
      'posts' => $posts
    ]);
  }
}

Simple Blog list view

我已将一个变量传递给我的路由,因此我可以单击博客文章并让它呈现与正确的“id”对应的视图

$app->get('/', 'StoryListController:index')->setName('home');

$app->get('/stories/story/{postId}', 'StoryTemplateController:story')->setName('stories.story');

但在我的一生中,我无法正确访问控制器中的该变量。这是我尝试过的,不确定我是否朝着正确的方向前进。任何能让我重新开始的帮助都将不胜感激。

<?php

namespace App\Controllers;

use App\Models\User;
use App\Models\Post;
use Slim\Views\Twig as View;
use Illuminate\Database\Eloquent\Model;

class StoryTemplateController extends Controller
{
  public function story($request, $response, $postId)
  {
    $post = Post::join('users', 'posts.user_id', '=', 'users.id')->where('posts.id', '=', '{postId}')->with(['postId' => $postId])->get();
    return $this->container->view->render($response, '/stories/story.twig', [
      'post' => $post
    ]);
  }
}

【问题讨论】:

    标签: php laravel-5 eloquent slim


    【解决方案1】:

    您可以从Request 的属性中获取postId

    $postId = $request->getAttribute('postId');
    

    或者通过查看$args 的数组,这是传递给您的路由操作的第三个参数:

    $postid = $args['postId'];
    

    试试这个:

    <?php
    
    namespace App\Controllers;
    
    use App\Models\User;
    use App\Models\Post;
    use Slim\Views\Twig as View;
    use Illuminate\Database\Eloquent\Model;
    
    class StoryTemplateController extends Controller
    {
      public function story($request, $response, $args)
      {
        $postId = $requst->getAttribute('postId');
        // or $postId = $args['postId'];
    
        // rest of method
      }
    }
    

    【讨论】:

    • 准确!我有一种感觉,我把事情复杂化了。然后我可以使用$post = Post::findOrFail($postId); 将我的 $post 变量拉入我的模板中。
    • @RobAllen-我已经能够用我的两篇博客文章呈现“主页”视图,现在可以点击一篇文章来呈现“故事”视图,但它似乎只是在拉动第一条记录是点击博文1还是博文2。public function story($request, $response, $args) { $postId = $request-&gt;getAttribute('postId'); $post = Post::join('users', 'posts.user_id', '=', 'users.id')-&gt;where('posts.id', $postId)-&gt;findOrFail($postId); return $this-&gt;container-&gt;view-&gt;render($response, '/stories/story.twig', [ 'post' =&gt; $post ]); }
    • 这可能只是我没有正确插入树枝模板的问题,因为我可以使用 url /1 和 /2 查看这两个帖子,但“home”链接似乎只有指向 /1 {% for post in posts %} &lt;div class="post"&gt; &lt;h2&gt;&lt;a href="{{ path_for('stories.story', {'postId': post.id}) }}"&gt;{{ post.title }}&lt;/a&gt;&lt;/h2&gt; &lt;p&gt;{{ post.body[:50] }}&lt;/p&gt; &lt;div class="author"&gt; By {{ post.first_name }} &lt;/div&gt; &lt;/div&gt; {% endfor %}
    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多