我已经制作了一个表单来提交用户数据,但是当我单击“提交”按钮时,它显示错误“找不到对象”
查看文件”
@extends('layout')
@section('content')
<h1 class="title">Simple Form</h1>
<form method="POST" action="/projects">
@csrf
<div class="field">
<label class="label" for="name">Name</label>
<div class="control">
<input type="text" class="input" name="name" placeholder="Enter Name">
</div>
</div>
<div class="field">
<label class="label" for="email">E-mail</label>
<div class="control">
<input type="text" class="input" name="email" placeholder="Enter E-mail Address">
</div>
</div>
<div class="field">
<label class="label" for="pincode">Pincode</label>
<div class="control">
<input type="text" class="input" name="pincode" placeholder="Enter Pincode">
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button">Submit</button>
</div>
</div>
</form>
@endsection
控制器文件
<?php
namespace App\Http\Controllers;
use App\Project;
use Illuminate\Http\Request;
class ATGController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('projects.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store()
{
return request()->all();
}
/**
* Display the specified resource.
*
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function show(Project $project)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
//
}
}
路由文件
<?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::get('/projects','ATGController@index');
Route::get('/projects/create','ATGController@create');
Route::post('/projects','ATGController@store');
布局文件
<!DOCTYPE html>
<html>
<head>
<title>ATG</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
找不到对象! 在此服务器上找不到请求的URL。推荐页面上的链接似乎有误或已过时。请将该错误告知该页面的作者。 如果您认为这是服务器错误,请与网站管理员联系。 错误404 本地主机 Apache / 2.4.39(Win64)OpenSSL / 1.1.1c PHP / 7.3.7
答案 0 :(得分:1)
您无需在网址前使用 / 。 web.php:
Route::post('projects','ATGController@store');
查看文件:
<form method="POST" action="{{ url('projects') }}">
如果仍不能解决问题,请检查.env文件中的app_url。
答案 1 :(得分:0)
您可以简单地通过route()
帮助器来命名您的路线,然后通过route()
帮助器在表单的action属性中对其进行调用。这是有关laravel document命名路线的信息。
web.php
Route::get('projects','ATGController@index')->name('projects.index');
查看
<form method="POST" action="{{ route('projects.index') }}">