【问题标题】:POST Slim Route not workingPOST Slim Route 不起作用
【发布时间】:2016-01-09 21:22:20
【问题描述】:

我正在使用 Slim 进行开发。我所有的 GET 路由都工作得很好,但是每当我使用 POST 时,我都会得到“意外结果”。请看看我是如何实现 slim 和那个“意外错误”的。

index-routes.php(索引根文件)

<?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();
?>

路由/默认路由.php

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

通过 AJAX 调用的 POST 请求的来源

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

AJAX 调用 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);
      }
    });    
  }
}

“意外错误”:当我执行 try1() 时,POST 路由确实成功执行,但 site-index.php 的内容(整个纯文本代码)(我在根索引路由中调用了它)。 php 文件)也会随之记录。我首先导入 site-index.php 的原因是因为它就像我的网站的“主要舞台”。这是我要加载的唯一页面,用户可以在其中导航。

  • 我想知道:
    1. 为什么我会得到这种类型的输出?
    2. 我的方法好吗?我认为从 index-routes 导入我的主阶段文件会导致这种情况。有没有其他方法可以做到这一点?

感谢任何帮助。谢谢。

【问题讨论】:

    标签: javascript php ajax slim


    【解决方案1】:

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

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

    1. 将您的所有页面渲染嵌套在路由内,并且不会为 AJAX 路由渲染整个页面。
    2. 修改您的 AJAX 调用以搜索返回的 DOM 以找到相关信息。

    在您的示例中,AllHailSuccess! 将显示在site-index.php 中的所有内容之后

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

    这是我在项目中使用的 Viewer 类的简单示例

    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 并创建一个构造和返回响应对象的类。这将为您提供更大的灵活性和能力来设置 HTTP 状态代码和 HTTP Return 标头。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2022-01-06
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多