web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function() {
return view('welcome');
});
Route::post('/xyz','[email protected]');
xyz.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class xyz extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request) {
$name = $request->name;
$email = $request->email;
$password = $request->password;
return response()
->json(['name' => $name, 'email' => $email,'password' =>$password]);
}
}
在这里,我甚至没有使用表格,否则我可以插入cs rf_token。这个API有什么遗漏吗?
将您的路由移至'api.php'并使用'api'前缀进行访问。网络路由使用'web'中间件组,它需要一个有效的csrf标记与每个表单请求一起传递。除非你创建这条路线与你的前端使用ajax,否则你不需要csrf检查。 – Sandeesh
@Sandeesh我应该在网址或其他地方添加** api **前缀吗? –
默认情况下,在'api.php'文件中定义的任何路由将以'api'作为前缀。所以你的路线将变成'http:// localhost:8000/api/xyz' – Sandeesh