使用slim框架在php中创建友好的URL

I want to open profile page like www.website.com/slim/username (where username is unique) .

I am currently using slim php framework for routing GET requests. So when I open www.website.com/profile/username it properly executes the route defined for /username and gives simple json response like this

{"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/slim/username and receive data and display it with template.

Problem is that if a user navigates directly to www.website.com/slim/username, he will only see the raw JSON response.

I want users to see the JSON response with template when they directly make a GET request from there browser.

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(); or $isXHR = $app->request->isXhr(); to check the request is ajax or not. If not ajax than redirect to the page you want ( may be profile if logged in, or login page if not logged in )

if($isXHR){
   echo 'json';
}else{
   render view
}