Reputation: 2858
I have a problem. I am using slim and I have route for my main page:
$app->get('/', function() use ($app) { ...
In one of my controllers I want to redirect to the main page, so I write
$app->response->redirect('/', 303);
But instead of redirection to the '/' route I'm getting redirected to the root of my local server which is http://localhost/
What am I doing wrong? How should I use redirect method?
Upvotes: 23
Views: 48481
Reputation: 876
Slim 4:
$response->withHeader('Location', '/redirect/to');
Or in place of fixed string:
use Slim\Routing\RouteContext;
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login');
return $response->withHeader('Location', $url);
Slim Documentation: http://www.slimframework.com/docs/v4/objects/response.html#returning-a-redirect RouteContext: https://discourse.slimframework.com/t/redirect-to-another-route/3582
Upvotes: 6
Reputation: 2428
Slim 3
$app->get('/', function ($req, $res, $args) {
$url = 'https://example.org';
return $res->withRedirect($url);
});
Reference: https://www.slimframework.com/docs/v3/objects/response.html#returning-a-redirect
Upvotes: 15
Reputation: 3591
I think I faced a similar problem, and the issue was with my .htaccess config file. It should actually be something like this:
RewriteEngine On
RewriteBase /api #if your web service is inside a subfolder of your app,
# you need to pre-append the relative path to that folder, hope this helps you!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Upvotes: 0
Reputation: 757
For Slim v3.x:
Use $response->withStatus(302)->withHeader('Location', $url);
instead of $app->redirect();
In Slim v2.x one would use the helper function $app->redirect(); to trigger a redirect request. In Slim v3.x one can do the same with using the Response class like so (see the following example)[1].
Use pathFor()
instead of urlFor()
:
urlFor() has been renamed pathFor() and can be found in the router object. Also, pathFor() is base path aware[2].
Example:
$app->get('/', function ( $request, $response, $args ) use ( $app ) {
$url = $this->router->pathFor('loginRoute');
return $response->withStatus(302)->withHeader('Location', $url);
});
Note: additional parameters can be supplied by passing an associative array of parameter names and values as a second argument of pathFor()
like: $this->router->pathFor('viewPost', ['id' => 1]);
.
The router’s pathFor() method accepts two arguments:
- The route name
- Associative array of route pattern placeholders and replacement values[3]
References:
Upvotes: 4
Reputation: 12103
From Slim3 docs http://www.slimframework.com/docs/start/upgrade.html
$app->get('/', function ($req, $res, $args) {
return $res->withStatus(302)->withHeader('Location', 'your-new-uri');
});
Upvotes: 24
Reputation: 7411
Slim allows you to name routes, and then redirect back to them based upon this name, using urlFor()
. In your example, change your route to:
$app->get('/', function() use ($app) { ... })->name("root");
and then your redirection becomes:
$app->response->redirect($app->urlFor('root'), 303);
See Route Helpers in the Slim documentation for more information.
Upvotes: 23
Reputation: 3926
//Set Default index or home page
$app->get('/', function() use ($app) {
$app->response->redirect('login.php');
});
Upvotes: 5
Reputation: 2900
give your '/' route a name,
$app = new \Slim\Slim();
$app->get('/', function () {
echo "root page";
})->name('root');
$app->get('/foo', function () use ($app) {
$app->redirect($app->urlFor('root') );
});
$app->run();
This should give you the correct url to redirect
http://docs.slimframework.com/routing/names/ http://docs.slimframework.com/routing/helpers/#redirect
Upvotes: 7