简体   繁体   中英

Creating friendly url in php using slim framework

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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