在Laravel 3中扩展课程是否还需要其他步骤?
我创建了application/libraries/response.php
:
class Response extends Laravel\Response {
public static function json($data, $status = 200, $headers = array(), $json_options = 0)
{
$headers['Content-Type'] = 'application/json; charset=utf-8';
if(isset($data['error']))
{
$status = 400;
}
dd($data);
return new static(json_encode($data, $json_options), $status, $headers);
}
public static function my_test()
{
return var_dump('expression');
}
}
但由于某些原因,my_test()
函数或修改后的json()
函数都无效。
在我的控制器中,我执行以下操作:
Response::my_test();
// or
$response['error']['type'] = 'existing_user';
Response::json($response);
没有人工作,我错过了什么?
答案 0 :(得分:4)
您应首先添加名称空间 - 如下所示:
档案:application/libraries/extended/response.php
<?php namespace Extended;
class Response extends \Laravel\Response {
public static function json($data, $status = 200, $headers = array(), $json_options = 0)
{
$headers['Content-Type'] = 'application/json; charset=utf-8';
if(isset($data['error']))
{
$status = 400;
}
dd($data);
return new static(json_encode($data, $json_options), $status, $headers);
}
public static function my_test()
{
return var_dump('expression');
}
}
然后在config / application.php中你需要更改别名
'Response' => 'Extended\\Response',
然后在start.php
Autoloader::map(array(
'Extended\\Response' => APP_PATH.'libraries/extended/response.php',
));
答案 1 :(得分:1)
实际上,扩展库的正确方法如下:
response.php
application/libraries/
class Response extends \Laravel\Response
'Response' => 'Laravel\\Response'
application/config/application.php
醇>
经过测试,确实有效。这就是我现在这样做的方式