[英]Creating friendly url in php using slim framework
I want to open profile page like www.website.com/slim/username
(where username
is unique) . 我想打开个人资料页面,如www.website.com/slim/username
(其中username
是唯一的)。
I am currently using slim php
framework for routing GET
requests. 我目前正在使用slim php
框架来路由GET
请求。 So when I open www.website.com/profile/username
it properly executes the route defined for /username
and gives simple json response like this 因此,当我打开www.website.com/profile/username
它会正确执行为/username
定义的路由,并给出简单的json响应,如下所示
{"users":[{"id":"01","firstname":"kamron","lastname":"shaw","gender":"male","status":"Offline"}]}
From this point I wanted to integrate this data with the profile page template, so I made a profile page template at www.website.com/profile.php
and from there used ajax
to make a GET
request to 从这一点开始,我想将这些数据与个人资料页面模板集成在一起,因此我在www.website.com/profile.php
创建了个人资料页面模板,然后使用ajax
向GET
请求发送
www.website.com/slim/username
and receive data and display it with template. www.website.com/slim/username
并接收数据并与模板一起显示。
Problem is that if a user navigates directly to www.website.com/slim/username
, he will only see the raw JSON
response. 问题是,如果用户直接导航到www.website.com/slim/username
,则他只会看到原始的JSON
响应。
I want users to see the JSON
response with template when they directly make a GET
request from there browser. 我希望用户直接从那里的浏览器发出GET
请求时,看到带有模板的JSON
响应。
How can I achieve this? 我该如何实现? Please correct me if I am wrong anywhere and tell me a suitable way of doing this. 如果我在任何地方错了,请纠正我,并告诉我这样做的合适方法。
Use $isXHR = $app->request->isAjax();
使用$isXHR = $app->request->isAjax();
or $isXHR = $app->request->isXhr();
或$isXHR = $app->request->isXhr();
to check the request is ajax or not. 检查请求是否为ajax。 If not ajax than redirect to the page you want ( may be profile if logged in, or login page if not logged in ) 如果不是ajax,则重定向到所需页面(如果已登录,则可以是个人资料;如果未登录,则可以是登录页面)
if($isXHR){
echo 'json';
}else{
render view
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.