Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Slim框架转发HTTP请求_Php_Slim_Http Redirect_Forward - Fatal编程技术网

Php 如何使用Slim框架转发HTTP请求

Php 如何使用Slim框架转发HTTP请求,php,slim,http-redirect,forward,Php,Slim,Http Redirect,Forward,是否可以在Slim中转发请求? 与JavaEE中一样,“转发”的含义是在内部重定向到另一个路由,而不将响应返回给客户端并维护模型 例如: $app->get('/logout',function () use ($app) { //logout code $app->view->set("logout",true); $app->forward('login'); //no redirect to client please })->name("l

是否可以在Slim中转发请求? 与JavaEE中一样,“转发”的含义是在内部重定向到另一个路由,而不将响应返回给客户端并维护模型

例如:

$app->get('/logout',function () use ($app) {
   //logout code
   $app->view->set("logout",true);
   $app->forward('login'); //no redirect to client please
})->name("logout");

$app->get('/login',function () use ($app) {
   $app->render('login.html');
})->name("login");

我想你必须改变他们的方向。斯利姆没有前锋。但您可以在重定向函数中设置状态代码。当你重定向到一个路由时,你应该得到你想要的功能

// With route
$app->redirect('login');
// With path and status code
$app->redirect('/foo', 303);
以下是文档中的一个示例:

<?php
$authenticateForRole = function ( $role = 'member' ) {
    return function () use ( $role ) {
        $user = User::fetchFromDatabaseSomehow();
        if ( $user->belongsToRole($role) === false ) {
            $app = \Slim\Slim::getInstance();
            $app->flash('error', 'Login required');
            $app->redirect('/login');
        }
    };
};
重定向()
方法。但是,它会发送一个您不想要的
302临时重定向
响应

$app->get("/foo", function () use ($app) {
    $app->redirect("/bar");
});
另一种可能性是
pass()。调用
pass()
时,Slim将立即停止处理当前匹配路由并调用下一个匹配路由

如果未找到后续匹配路由,则会向客户端发送
404 Not found

$app->get('/hello/foo', function () use ($app) {
    echo "You won't see this...";
    $app->pass();
});

$app->get('/hello/:name', function ($name) use ($app) {
    echo "But you will see this!";
});

在我看来,最好的方法是使用Slim的内部路由器()功能并调度()匹配的路由(意思是:在不重定向的情况下从匹配的路由执行可调用路由)。我想到了几个选项(取决于您的设置):

1.调用命名route+callable不需要任何参数(您的示例) 这肯定会对你有帮助,但我仍然想展示其他场景


2.调用命名路由+可使用参数调用 上面的例子将失败。。。因为现在我们需要将参数传递给可调用

   // getting the named route
   $route = $app->router()->getNamedRoute('another_route');

   // calling the function with an argument or array of arguments
   call_user_func($route->getCallable(), 'argument');
调度路由(使用$route->dispatch())将调用所有中间件,但这里我们只是直接调用可调用的。。。为了得到完整的包装,我们应该考虑下一个选项…


3.呼叫任何路线 如果没有命名路由,我们可以通过查找与http方法和模式匹配的路由来获得路由。为此,我们将重新加载设置为
TRUE

   // getting the matched route
   $matched = $app->router()->getMatchedRoutes('GET','/classes/name', true);

   // dispatching the (first) matched route
   $matched[0]->dispatch(); 
在这里,您可能需要添加一些检查,例如在没有匹配的路由的情况下发送
notFound
。 我希望你明白我的意思

   // getting the matched route
   $matched = $app->router()->getMatchedRoutes('GET','/classes/name', true);

   // dispatching the (first) matched route
   $matched[0]->dispatch();