如何使用SLIM Framework PHP传递URL中的ID?

问题描述 投票:0回答:3

我是新手使用SLIM框架,我正在开发一个项目/平台,允许用户发布他们的列表并从潜在客户那里预订。但我希望他们能够分享他们列表的链接。

我在下面提供了相关代码,用于在一个页面上显示所有这些列表。单击其中一个列表时,信息将被推送到模式中。我希望为每个列表生成一个链接,可以通过电子邮件或消息共享,并轻松访问。

在我坚持的那一刻:

localhost/listings

我期望的结果是:

localhost/listings/+itemid/

Listings.html

function listingInfo($id,$location){

  // Detail View Specific

  $('.listing-info-container').addClass('active');
  $('.listing-info').html('').removeClass('active');
  showLoader($('.listing-info'));
  showLoader($('.listings-results'));

  setTimeout(function(){
    $.ajax({
      method: "GET",
      url: "/listing_information/"+$id
    })
    .done(function(result){
      $('.listing-info').html(result).addClass('active');
      hideLoader($('.listing-info'));
      hideLoader($('.listings-results'));
      // hide map if on mobile - link to external google maps site instead
      $('.listings-map').toggleClass('is-hidden', isMobile());
      // set co-ord vals in hidden textbox
      $('#listing-detail-directions-ll').val($location.toString().replace(/["'()]/g,'').replace(' ', ''));
    });
  }, 1000);

Listings.php

   //to display all listings on a single page
$app->get('/listings', function ($request, $response, $args) {
      $variables['title'] = 'Listings';
      $variables['categories'] = $this->db->select('listing_categories','*');

      return $this->view->render( $response, 'listings.html', $variables,);
    });
php slim slim-3
3个回答
3
投票

只需提及路由描述中的参数,如下所示:

$app->get('/listings/{id}', function ($request, $response, $args) {
...

然后你就可以在路由处理程序中访问它了:

$route = $request->getAttribute('route');
$listingId = $route->getArgument('id');

1
投票

这是你想要的?

$app->get('/api/:version/users/:id', function ($version, $id) {
    // you can access $version and $id here
}

0
投票

在Slim v3上执行此操作的最简单方法是:

$app->get('/listings/{id}', function ($request, $response, $id) {
    // do something with $id here
}

由于参数值被{id}捕获并存储在$ id变量中。

© www.soinside.com 2019 - 2024. All rights reserved.