dongliang_bj2016 2016-03-29 12:02
浏览 46

使用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.

  • 写回答

1条回答 默认 最新

  • dqy012345 2016-03-29 12:10
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条