我正在与laravel RestfulApi项目合作。我面临着意外的麻烦。当我尝试将API请求发送到api.php路由时,它转到了web.php路由。但是,如果我不在控制器文件中使用验证,则代码运行良好。使用验证时,只会出现上述问题。下面是我的代码。
Api.php路由
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/showingData','UserContactController@show');
Route::post('/storingData','UserContactController@store');
Web.php路由
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
UserContactController.php
<?php
namespace App\Http\Controllers;
use App\UserContact;
use Illuminate\Http\Request;
class UserContactController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required|max:5'
]);
$userContact=new UserContact();
$userContact->name=$request->input('name');
$userContact->email=$request->input('email');
$userContact->description=$request->input('description');
$userContact->visibility=$request->input('visibility');
$userContact->created_by=$request->input('created_by');
$userContact->save();
return response()->json([
"message"=>"student record created"
],201);
}
}
UserContac.php模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserContact extends Model
{
//
}
----------
我的邮递员网址
http://amaderproject.test/api/storingData