<?php namespace App\Http\Controllers\Test; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class TestController extends Controller { // public function create() { return view('test.create'); } public function store(Request $request) { $namespace = $request->input('namespace'); $className = $request->input('className'); $action = $request->input('action'); $param = $request->input('param'); $class = ($className == "") ? $namespace : $namespace.'\\'.$className; $class = str_replace('/' , '\\', $class); $object = new $class(); $param = ($param == "")? [] : explode("|",$param); $data = call_user_func_array([$object, $action], $param); return (is_array($data)) ? json_encode($data) : dd($data); } }
视图:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>不区分大小写(可以自行完善)</h1> <br> <form class="" action="{{route('test.store')}}" method="post"> @csrf() 命名空间:<input type="text" value='' style="width:300px" name='namespace' placeholder="如:app\index\controller 或app\index\controller\Index">可以写全,然后下面类名不用些<br> 类名:<input type="text" name='className' placeholder="如:index ">命名空间全可以不用写<br> 测试方法名:<input type="text" name='action' placeholder="index"><br> 传递参数以 | 分割:<input type="text" placeholder="如: 1|2|3" name='param'><br> <input type="submit" name="" value="测试"/> </form> </body>