我在support\Response 增加了几个方法,一直用的好好的,但是前天升级了一下依赖之后就报错了,一直没有找到原因...麻烦大家帮忙看看
Error: Call to undefined method support\Response::withSuccess() in /Users/gavin/Sites/triptain/erp/app/controller/console/UserController.php:284
Stack trace:
#0 /Users/gavin/Sites/triptain/erp/vendor/workerman/webman-framework/src/App.php(253): app\controller\console\UserController->role_add(Object(support\Request), '26', '168', 'dandang')
#1 /Users/gavin/Sites/triptain/erp/app/middleware/AuthCheck.php(24): Webman\App::Webman\{closure}(Object(support\Request))
#2 /Users/gavin/Sites/triptain/erp/vendor/workerman/webman-framework/src/App.php(245): app\middleware\AuthCheck->process(Object(support\Request), Object(Closure))
#3 /Users/gavin/Sites/triptain/erp/app/middleware/OwnerCheck.php(22): Webman\App::Webman\{closure}(Object(support\Request))
#4 /Users/gavin/Sites/triptain/erp/vendor/workerman/webman-framework/src/App.php(245): app\middleware\OwnerCheck->process(Object(support\Request), Object(Closure))
#5 /Users/gavin/Sites/triptain/erp/app/middleware/Back.php(25): Webman\App::Webman\{closure}(Object(support\Request))
#6 /Users/gavin/Sites/triptain/erp/vendor/workerman/webman-framework/src/App.php(245): app\middleware\Back->process(Object(support\Request), Object(Closure))
#7 /Users/gavin/Sites/triptain/erp/vendor/workerman/webman-framework/src/App.php(330): Webman\App::Webman\{closure}(Object(support\Request))
#8 /Users/gavin/Sites/triptain/erp/vendor/workerman/webman-framework/src/App.php(147): Webman\App::findRoute(Object(Workerman\Connection\TcpConnection), '/console/user/2...', 'GET/console/use...', Object(support\Request))
#9 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Connection/TcpConnection.php(638): Webman\App->onMessage(Object(Workerman\Connection\TcpConnection), Object(support\Request))
#10 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Events/Select.php(295): Workerman\Connection\TcpConnection->baseRead(Resource id #356)
#11 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(2431): Workerman\Events\Select->loop()
#12 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(1555): Workerman\Worker->run()
#13 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(1397): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#14 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(1371): Workerman\Worker::forkWorkersForLinux()
#15 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(1692): Workerman\Worker::forkWorkers()
#16 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(1641): Workerman\Worker::monitorWorkersForLinux()
#17 /Users/gavin/Sites/triptain/erp/vendor/workerman/workerman/Worker.php(551): Workerman\Worker::monitorWorkers()
#18 /Users/gavin/Sites/triptain/erp/start.php(108): Workerman\Worker::runAll()
#19 {main}
1. 控制器 UserController.php
//为员工添加一个角色
public function role_add(Request $request, $user_id, $shop_id, $role){
$roles = config('site.user_roles_erp');
if(!in_array($role, $roles)){
return show_404();
}
$user_role = ShopUser::query()
->where('client_id', session('client_id'))
->where('user_id', $user_id)
->where('shop_id', $shop_id)
->where('role', $role)
->first();
if($user_role){
return back()->withError('非法操作');
}
//插入记录
$user_role = new ShopUser;
$user_role->client_id = session('client_id');
$user_role->user_id = $user_id;
$user_role->shop_id = $shop_id;
$user_role->role = $role;
$user_role->save();
return back()->withSuccess('操作成功');
}
2. support/Response.php
namespace support;
/**
* Class Response
* @package support
*/
class Response extends \Webman\Http\Response
{
/**
* @param array $data
* @return $this
*/
public function withInput(array $data = []): Response
{
if(empty($data)){
$data = request()->post();
}
$session = session();
$session->set('old', $data);
return $this;
}
/**
* @param string $message
* @return $this
*/
public function withError(string $message): Response
{
session(['error_message' => $message]);
return $this;
}
/**
* @param string $message
* @return $this
*/
public function withSuccess(string $message): Response
{
session(['success_message' => $message]);
return $this;
}
}
几个中间件
3.1 AuthCheck.php
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AuthCheck implements MiddlewareInterface
{
/**
* @param Request $request
* @param callable $next
* @return Response
*/
public function process(Request $request, callable $next): Response
{
$session = $request->session();
//验证是否登录
if (!$session->has('user_id')) {
return redirect('/auth/login');
}
return $next($request);
}
}
3.2 OwnerCheck.php
<?php
namespace app\middleware;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
class OwnerCheck implements MiddlewareInterface
{
public function process(Request $request, callable $next): Response
{
$session = $request->session();
//验证是否登录
if(!$session->has('user_id')){
return redirect('/auth/login');
}
//验证角色
if($session->has('is_owner') && $session->get('is_owner') == 1){
return $next($request);
}
return \response('<h2 style="text-align: center;padding-top: 1em;">403 Forbidden</h2><p style="text-align: center;"><a href="/">返回首页</a></p>', 403);
}
}
3.3 Back.php
<?php
namespace app\middleware;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
class Back implements MiddlewareInterface
{
public function process(Request $request, callable $next): Response
{
if(!$request->isAjax()){
$session = $request->session();
$urls = (array)$session->get('urls');
if(count($urls) === 2){
array_shift($urls);
}
$urls[] = $request->uri();
$session->set('urls', $urls);
}
return $next($request);
}
}
把你的support/Response.php 放到support目录下试下
一直就是在这个目录
它应该是加载到 vendor/workerman/webman-framework/src/support/Response.php 去了,删掉这个试下。
的确是,我把vendor/workerman/webman-framework/src/support这个目录都删了,好了
谢谢老大