简体   繁体   English

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

[英]How can I pass the ID in the URL using SLIM Framework PHP?

I'm new to using SLIM framework, I'm working on a project/platform that allows users to post their listings and take bookings from potential clients. 我是使用SLIM框架的新手,我正在开发一个项目/平台,允许用户发布其列表并接受潜在客户的预订。 But I want them to be able to share a link to their listing. 但我希望他们能够共享指向他们列表的链接。

I have provided the relevant code below, that is used to display all these listings on a single page. 我在下面提供了相关的代码,该代码用于在单个页面上显示所有这些列表。 When clicking on one of the listings, the information is pushed into a modal. 当单击其中一个列表时,信息将被推送到模式中。 I'm looking to generate a link for each listing that can be shared via email or message and accessed easily. 我正在为每个列表生成链接,可以通过电子邮件或消息共享这些链接,并且可以轻松访问。

At the minute I'm stuck with: 在这一刻,我坚持:

localhost/listings

My desired outcome is: 我期望的结果是:

localhost/listings/+itemid/

Listings.html 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 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,);
    });

Just mention the parameter in the route description like this: 只需在路由说明中提及如下参数:

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

Then you'll be able to access it within the route handler: 然后,您将可以在路由处理程序中访问它:

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

Is this what you are looking for? 这是你想要的?

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

The simplest way to do this on Slim v3 is: 在Slim v3上执行此操作的最简单方法是:

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

Since the parameter value gets captured by {id} and stored in the $id variable. 由于参数值被{id}捕获并存储在$ id变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM