doujuegai8830 2017-02-10 07:35
浏览 175
已采纳

使用withAttribute错误的PHP Slim Framework请求

I am just try to pass username from middileware auth function

$request->withAttribute('username','XXXXXX');
return $next($request, $response);

But i cannot access this username using

$request->getAttribute('username');

I found a solution that its working only when i add like this

 return $next($request->withAttribute('username','XXXXXX'), $response);

What is the reason? Please help me. I need to pass multiple arguments pass. What should i do?

  • 写回答

2条回答 默认 最新

  • doushan1850 2017-02-10 07:58
    关注

    withAttributes does not change the state of this object.

    Excerpt from relevant source code

    public function withAttribute($name, $value)
    {
        $clone = clone $this; 
        $clone->attributes->set($name, $value);
        return $clone;
    }
    

    For testing purposes, in your slim fork, change above code as this.

    /* 
    * Slim/Http/Request.php 
    */
    public function withAttribute($name, $value)
    {
    
        $this->attributes->set($name, $value);
        return $this;
    }
    

    Then return $next($request, $response); will work as you expected.

    Demo code for inspection

    <?php 
     /* code taken from - https://www.tutorialspoint.com/php/php_object_oriented.htm*/
     class Book {
    
          var $price;
          var $title;
    
    
          function setPrice($par){
             $this->price = $par;
          }
    
          function getPrice(){
             return $this->price;
          }
    
          function setTitle($par){
             $this->title = $par;
          }
    
          function getTitle(){
             return $this->title;
          }
       }
    
        class CopyBook {
    
          var $price;
          var $title;
    
          function setPrice($par){
             $clone = clone $this;
             $clone->price = $par;
          }
    
          function getPrice(){
             return $this->price;
          }
    
          function setTitle($par){
             $clone = clone $this;
             $clone->title = $par;
          }
    
          function getTitle(){
             return $this->title;
          }
       }
    
       $pp = new Book;
       $pp->setTitle('Perter Pan');
       $pp->setPrice(25);
    
       $cpp = new CopyBook;
    
       $cpp->setTitle('Peter Pan');
       $cpp->setPrice(25);
    
       var_dump($pp);
       var_dump($cpp);
    
       ?>
    

    Result:

     object(Book)#1 (2) {
      ["price"]=>
      int(25)
      ["title"]=>
      string(10) "Peter Pan"
    }
    object(CopyBook)#2 (2) {
      ["price"]=>
      NULL
      ["title"]=>
      NULL
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况