Action.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Http\Request;
  5. use Mockery\Exception;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class Action extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'action:call {uri}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. * @param OutputInterface $output
  33. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  34. */
  35. public function handle(Request $request)
  36. {
  37. $url = $this->argument('uri');
  38. // 获取控制器+方法
  39. $uri = parse_url($url, PHP_URL_PATH);
  40. $uri = explode('@', $uri);
  41. // 获取参数
  42. parse_str(parse_url($url, PHP_URL_QUERY), $param);
  43. $controller = $uri[0] ?? '';
  44. $action = $uri[1] ?? '';
  45. if (empty($controller) || empty($action)) {
  46. $this->info('The format (Controller@method) is required');
  47. return;
  48. }
  49. try {
  50. $container = app()->make("App\Http\Controllers\\" . $controller);
  51. } catch (Exception $e) {
  52. $this->info($e->getMessage());
  53. return;
  54. }
  55. if ($param) {
  56. foreach ($param as $k => $v) {
  57. $request->offsetSet($k, $v);
  58. }
  59. }
  60. $result = app()->call([$container, $action], $param);
  61. $this->info($result);
  62. }
  63. }