簡體   English   中英

如何使用SLIM Framework PHP在URL中傳遞ID?

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

我是使用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,);
    });

只需在路由說明中提及如下參數:

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

然后,您將可以在路由處理程序中訪問它:

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

這是你想要的?

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

在Slim v3上執行此操作的最簡單方法是:

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

由於參數值被{id}捕獲並存儲在$ id變量中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM