我在使用这个 slim3 php 代码时遇到了问题。在函数 createErrorReponse 函数中,$response->getBody() 为 null 或空。 PHP 在下面抱怨以下错误。如您所见, getBody() 大小为空,因此 write 无法处理它。不过,同一行也适用于其他功能。
HTTP/1.1 200 OK 内容类型:text/html;字符集=UTF-8 0
致命错误:在第 16 行的 /home/ubuntu/webapp/middleware/authmodule.php 中的非对象上调用成员函数 withHeader()
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class AuthenticationMiddleware { public function isAuthenticated($userid, $authorization) { //do data validation here return false; } public function createErrorResponse($code, $msg, $response) { echo $response; echo $response->getBody()->getSize(); $response = $response->getBody()->write(json_encode('holla')); $response = $response->withHeader('Content-Type', 'application/json; charset=utf-8'); return $response; } public function __invoke($request, $response, $next) { $userid = $request->getHeaderLine('userid'); $authorization = $request->getHeaderLine('Authorization'); if($this->isAuthenticated($userid, $authorization)) { $response = $next($request, $response); } else { $msg = 'You are unauthenticated. Please login again'; $code = 400; $response = $this->createErrorResponse($code, $msg, $response); } return $response; } } |
感谢您提供有关错误报告和修复的提示。
我觉得有必要回答这个问题,以防万一你还没有完全放弃 PHP 和 Slim 框架。
希望对其他人有所帮助。
我的做法是:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use Slim\\Http\ equest; use Slim\\Http\ esponse; class AuthenticationMiddleware { public function isAuthenticated($userid, $authorization) { //do data validation here return false; } public function createErrorResponse($code, $msg, Response $response) { return $response->withStatus($code) ->withHeader('Content-Type', 'application/json;charset=utf-8') ->withJson($msg); } public function __invoke(Request $request, Response $response, $next) { $userid = $request->getHeaderLine('userid'); $authorization = $request->getHeaderLine('Authorization'); if(!$this->isAuthenticated($userid, $authorization)) { $msg = 'You are unauthenticated. Please login again'; $code = 400; $this->createErrorResponse($code, $msg, $response); } else { $response = $next($request, $response); } return $response; } } |
我就这么说吧。我会在这段代码中抽象一些东西,这样我就不会重复自己了。我看到你在重复:
2 3 4 5 | return $response->withStatus($code) ->withHeader('Content-Type', 'application/json;charset=utf-8') ->withJson($msg); } |
在你所有的中间件中,也许在你的路由中。
希望这能让某人走上正轨。
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://
我正在处理http://prepwork.appacademy.io/mini-curriculum/array/中概述的数组问题我正在尝试创建函数my_transpose,它接受一个矩阵并返回其转置。我对写入二维数组感到很困惑!这是一个代码片段,突出了我的困惑。rows=[[0,1,2],[3,4,5],[6,7,8]]columns=Array.new(3,Array.new(3))putscolumns.to_s#Outputisa3x3arrayfilledwithnilcolumns[0][0]=0putscolumns.to_s#Outputis[[0,nil,nil],[
我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia
考虑这个,它工作正常::>.to_proc.curry(2)[9][8]#=>true,because9>8然而,即使>是一个二元运算符,如果没有指定的元数,上面的代码将无法工作::>.to_proc.curry[9][8]#=>ArgumentError:wrongnumberofarguments(0for1)为什么两者不等价?注意:我特别想用提供的一个参数创建中间柯里化(Currying)函数,然后然后调用然后用第二个参数调用它。 最佳答案 curry必须知道传入的过程的数量,对吧?:-1来自arity的负值令人困惑,但基本上
在Ruby(或Rails)中,我们可以做到new_params=params.merge({:order=>'asc'})现在new_params是一个带有添加键:order的散列。但是是否有一行可以返回带有已删除key的散列?线路new_params=params.delete(:order)不会工作,因为delete方法返回值,仅此而已。我们必须分3步完成吗?tmp_params=paramstmp_params.delete(:order)returntmp_params有没有更好的方法?因为我想做一个new_params=(params[:order].blank?||para