【发布时间】:2015-06-24 19:25:28
【问题描述】:
我有一个个人资料页面,登录用户可以在其中发送好友请求。这是一个(树枝)视图:
{% extends 'templates/default.php' %}
{% block title %}{{ user.getFullNameOrUsername }}{% endblock %}
{% block content %}
<h2>{{ user.username }}</h2>
{% if auth %}
<input type="submit" value="Send Friend Request" id="sfr">
<br>
{% endif %}
<img src="" alt="Profile picture for {{ user.getFullNameOrUsername }}">
<dl>
{% if user.getFullName %}
<dt>Full Name</dt>
<dd>{{ user.getFullName }}</dd>
{% endif %}
<dt>Email</dt>
<dd>{{ user.email }}</dd>
</dl>
{% endblock %}
这个页面的位置是:
http://localhost/authentication/public/u/martin 但在文件目录中为 C:\xampp2\htdocs\authentication\app\views\user\profile.php
一旦用户点击“发送好友请求”按钮,就会运行以下js:
$( document ).ready(function() {
$("#sfr").click(function( event ) {
event.preventDefault();
$.ajax({
method: "POST",
url: "http://localhost/authentication/app/routes/account/friend.php",
data: { fr: "sfr"}
})
.done(function( msg ) {
console.log(msg);
alert( msg );
});
});
});
我创建了一条名为friend的路线,它看起来像这样:
<?php
$app->post('/profile', $authenticated(), function() use ($app) {
$app = \Slim\Slim::getInstance();
$request = $app->request();
$fr = $request->post('fr');
echo "Friend Request sent";
})->name('account.friend.post');
我收到以下错误消息:
<br />
<b>Notice</b>: Undefined variable: app in <b>C:\xampp2\htdocs\authentication\app\routes\account\friend.php</b> on line <b>3</b><br />
<b>Fatal error</b>: Call to a member function post() on null in <b>C:\xampp2\htdocs\authentication\app\routes\account\friend.php</b> on line <b>3</b><br />
我认为问题出在 ajax 调用中的 url 中。我希望我可以将它路由到路由的名称而不是它的位置。
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
您必须使用
require 'path_to_slim/Slim.php'; \Slim\Slim::registerAutoloader();包含苗条框架