POST 瘦身路线不工作

POST Slim Route not working

我正在使用 Slim 进行开发。我所有的 GET 路由都工作得很好,但每当我使用 POST 时,我都会得到 "unexpected result"。请看看我是如何实现 slim 和 "unexpected error".

index-routes.php (index root file)

<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array(
'debug' => true
));
require_once 'site-index.php';
require_once 'routes/default-routes.php';
$app->contentType('application/json');
$app->run();
?>

routes/default-routes.php

<?php
$app->post('/login',function(){
    echo 'AllHailSuccess!';
    })
?>

origin of POST request called via AJAX

function try1()
{
    var value1 = "afsfesa";
    API.call('/login','text','POST',function(data){console.log(data)},{var1:value1});
}

AJAX Call API

var API = {
  call:function(url,returnType,reqType,callback,data){
    var data = (!!data) ? data : {};
    var callback = (!!callback) ? callback : function(){};
    $.ajax({
      dataType: returnType,
      type:reqType,
      crossDomain: true,
      xhrFields: { withCredentials: true }, 
      url: url,
      data:data,
      success:callback,
      error:function(data){
          console.log("Error!");
        console.log(data);
      }
    });    
  }
}

"Unexpected error":当我执行 try1() 时,POST 路径确实执行成功,但是 site-index.php 的内容(纯文本的整个代码)(我在 root index-routes.php 文件中调用的)也被一起记录下来。我首先导入 site-index.php 的原因是因为它对我的站点来说就像一个 "main stage"。这是我要加载的唯一页面,用户会在其中导航。

感谢任何帮助。谢谢。

您的 Slim 调用将return 页面上显示的任何内容。

有几种方法可以解决此问题:

  1. 将所有页面渲染嵌套在路由中,不要为 AJAX 路由渲染完整页面。
  2. 修改您的 AJAX 调用以搜索 returned DOM 以查找相关信息。

在您显示的示例中,AllHailSuccess! 将在 site-index.php

中的所有内容之后显示

许多人使用模板软件呈现他们的页面,然后使用服务通过模板呈现他们的页面。对于更基本的站点,我建议您创建一个简单的服务来显示内容。

这是我在项目中使用的 Viewer class 的一个简单示例

class Viewer {
  /**
   * Display the specified filename using the main template
   * @param   string $filepath The full path of the file to display
   */
  public function display($filepath) {
    //set a default value for $body so the template doesn't get angry when $body is not assigned.
    $body = "";
    if (file_exists($filepath)) {
      $body = get_include_contents($filepath);
    } else {
      //You want to also return a HTTP Status Code 404 here.
      $body = get_include_contents('404.html');
    }
    //render the page in the layout
    include('layout.php');
  }
}

/**
 * Gets the contents of a file and 'pre-renders' it.
 * Basically, this is an include() that saves the output to variable instead of displaying it.
 */ 
function get_include_contents($filepath, $params = array()) {
  if (is_file($filepath)) {
    ob_start();
    include $filepath;
    $ret = ob_get_contents();
    ob_end_clean();
    return $ret;
  }
  return false;
}

您希望向用户显示页面布局的路由现在应如下所示:

$app->get('/', function() {
  (new Viewer())->display('home.html');
});

这绝不是一个全面的解决方案,因为它没有解决正确的 HTTP 状态代码,并且在您的代码中直接引用文件可能会变得混乱,但它是一个很好的起点,并且可以快速模拟类似这样的东西.

如果你想继续朝这个方向发展,我建议你看一下 Slim v2 Response Documentation 并创建一个 class 来构造和 returns Response objects.这会给你更多的灵活性和权力来设置 HTTP 状态代码和 HTTP Return headers。

我强烈建议您也查看 Slim v3 Responses,因为 Slim 3 使用 PSR-7 响应 objects,这是跨多个框架的标准。