【发布时间】:2022-01-11 03:22:25
【问题描述】:
我正在使用 Cartalyst\Sentinel 和 Twig 构建一个框架应用程序,我的示例在这里:https://www.sitepoint.com/removing-the-pain-of-user-authorization-with-sentinel/。我只是更新了依赖,但原理是一样的。
在我的 twig 模板中,我尝试使用 jQuery 添加一个 XHR-Call:
{% for user in users %}
<script type="text/javascript">
$( document ).ready(function() {
$("#changeRole_{{ user.id }}").on('change', function() {
$.post( "../application/ajax/changeUserRole.php", { userId: {{ user.id }}, roleId: this.value })
.done(function( msg ) {
alert( msg );
}
);
});
});
</script>
{% endfor %}
问题是路由器试图解析文件路径“../application/ajax/changeUserRole.php”。所以我的问题是如何避免这种情况?也许还有一种方法可以在我的 router.php 中定义额外的 XHR-Routes。
让我们看看我的 router.php(这是我用来定义路由的方式):
$app->get('/login', function (Request $request, Response $response) {
$view = Twig::fromRequest($request);
$view->render($response, 'login.html.twig');
return $response;
});
【问题讨论】:
-
URL 是根据 HTTP 规则解析的,简单明了。您在 AJAX 中所做的任何事情都会被翻译为 HTTP 请求。不能只定义一个普通路由并在 AJAX 中使用吗?
-
^这个或发布你的htaccess