我在 Laravel 5.3 中创建了一个 request 类。看起来像这样:
<?php
namespace App\Http\Requests\User;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:25|min:2'
];
}
}
但是当我在我的 Controller 中使用它时,我收到:
NotFoundHttpException in RouteCollection.php line 161:
当我删除规则时,它看起来像这样:
public function rules()
{
return [
];
}
它突然起作用了!?!?
--更新--
看起来验证器看起来在我的 web.php 中,但我在那里没有路由(这就是我收到异常的原因:RouteCollection.php 第 161 行中的 NotFoundHttpException:)。我在 5.2 中没有这个问题。我应该怎么办?
--更新--
我当前的路线:
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-----------------------------------------------+--------------+---------------------------------------------------+------------+
| | POST | api/login | | App\Http\Controllers\Auth\AuthController@login | api |
| | GET|HEAD | api/register/{user}/verify/{ConformationCode} | | App\Http\Controllers\Auth\AdminController@confirm | api |
| | GET|HEAD | api/user | user.index | App\Http\Controllers\User\UserController@index | api,active |
| | POST | api/user | user.store | App\Http\Controllers\User\UserController@store | api,active |
| | GET|HEAD | api/user/create | user.create | App\Http\Controllers\User\UserController@create | api,active |
| | GET|HEAD | api/user/{user} | user.show | App\Http\Controllers\User\UserController@show | api,active |
| | PUT|PATCH | api/user/{user} | user.update | App\Http\Controllers\User\UserController@update | api,active |
| | DELETE | api/user/{user} | user.destroy | App\Http\Controllers\User\UserController@destroy | api,active |
| | GET|HEAD | api/user/{user}/edit | user.edit | App\Http\Controllers\User\UserController@edit | api,active
另外,如果尝试使用全新的 Laravel 5.3 安装,我会收到错误消息!
可能与以下内容有关:
在 Laravel 5.3 UserStoreRequest
扩展 FormRequest
(使用 Illuminate\Foundation\Http\FormRequest;)
并且在 Laravel 5.2 UserStoreRequest
扩展请求
(使用 App\Http\Requests\Request;)
--编辑--
API.PHP
<?php
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/login', 'Auth\AuthController@login');
Route::get('/register/{user}/verify/{ConformationCode}', 'Auth\AdminController@confirm');
Route::put('/test', 'User\UserController@test');
Route::group(['middleware' => ['active']], function () {
Route::resource('user', 'User\UserController');
Route::resource('corporation', 'Corporation\CorporationController');
Route::resource('forum', 'Forum\ForumController');
Route::resource('topic', 'Topic\TopicController');
});
存储方式
public function store(StoreRequest $request)
{
if($this->authorize($this->user))
{
$user = $this->userInfo->store($request);
event(new UserRegistered($user));
return response()->json(['success' => 'gebruiker succesvol aangemaakt. Welkom email verzonden.']);
}
return response()->json(['error' => 'niet geautoriseerd'], 401);
}
最佳答案
发生这种情况是因为 FormRequest::response 函数想要将您重定向到有错误的页面。但是因为您没有任何网络路由(只有 api 路由),它会失败并返回 NotFoundHttpException。如果您有任何网络路由,它会(在内部)将您重定向到根目录。
如果你看看里面的 FormRequest::response功能,你可以看到这一点:
if($this->expectsJson()) {
return new JsonResponse($errors, 422);
}
因此,如果您可以满足 expectsJson 中的期望,它就会按预期工作。
您可以通过多种方式做到这一点:
X-Requested-With header 确保请求是 XMLHttpRequest。application/json 提供一个 Accept header wantsJson 调用以始终返回 true。像这样:类 JsonRequest 扩展请求
{
公共(public)功能 wantsJson() {
返回真;
}
}
关于php - Laravel 5.3 抛出错误表单请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39752253/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie