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条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献