简体   繁体   English

使用Slim框架重定向

[英]Redirect with Slim Framework

I have the following code: 我有以下代码:

$app->get('/category/:name', function($name){
  //the render files are found under the templates folder
header('Location: searchPage.php?crs_category=$name');
});

The problem is that when I type /category/business its just lands on blank page. 问题是当我键入/category/business它仅落在空白页上。 I do not want to render the page because I can't exactly render searchPage.php?crs_category because it is view as a template and there's a work around to include to variable. 我不想呈现页面,因为我无法完全呈现searchPage.php?crs_category,因为它是作为模板查看的,并且可以解决将变量包含在内的问题。 I just want to go directly to that page while keeping the url clean. 我只想直接转到该页面,同时保持网址干净。 Essentially redirect to this page in the background with the url remaining that. 本质上是在后台重定向到此页面,并保留该URL。

Thanks in advance. 提前致谢。

Assuming you're using Apache as a webserver, couldn't you just rewrite the URL in .htaccess : 假设您将Apache用作网络服务器,是否不能只重写.htaccess的URL:

RewriteRule ^category/(.*)$ searchPage.php?crs_category=$1 [L,NC,QSA]

..or if you want a real redirect ..或者如果您想进行真正的重定向

RewriteRule ^category/(.*)$ searchPage.php?crs_category=$1 [L,NC,R=301]

PS If you insist on doing it in Slim, you could try PS:如果您坚持在Slim中进行操作,则可以尝试

$app->get('/category/:name', function($name) use ($app) {
    $app->response->redirect('/searchPage.php?crs_category='.$name, 303);
});

source: http://docs.slimframework.com/response/helpers/#redirect 来源: http : //docs.slimframework.com/response/helpers/#redirect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM