我是laravel5 Framework的新手。当我使用laravel5将数据插入数据库时,我得到的错误就像....
ClientFormRequest.php第10行中的FatalErrorException: 无法在类App \ Http \ Requests \ ClientFormRequest中使用静态方法Symfony \ Component \ HttpFoundation \ Request :: create()非静态
我的所有档案都在......
应用/ HTTP /控制器/ RegisterController.php
package View;
import java.awt.EventQueue;
public class HighBidMain {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame().setVisible(true);
}
});
}
}
应用/ HTTP /请求/ ClientFormRequest.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
use Illuminate\Http\Request;
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
路线
<?php namespace App\Http\Requests;
use Stringy\create;
use App\User;
use Validator;
use App\Http\Requests\ClientFormRequest;
class ClientFormRequest extends Request {
/**
* 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()
{
}
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}
public function create(array $data)
{
return client::create([
'fullname' => $data['fullname'],
'email' => $data['email'],
]);
}
}
答案 0 :(得分:1)
首先,我建议你反复观看Laravel 5 Fundamentals,因为它是免费的。其他系列也提供了很好的信息。
其次,我建议你至少使用Sublime Text和一些有用的包,以便能够检查系统文件(命名空间,接口,继承树等等)的深度嵌套关系。如果您不能/可能不会,这位朋友将随时为您服务Laravel API
第三,AFAIK,Laravel Request构建在Symfony的请求组件上。由于您尝试将其核心功能之一作为非静态函数重载,因此您收到此错误。
另外,老实说,我不会将我的用户/客户端模型创建逻辑放入请求中。 Laravel为这种误解提供了一个很好的例子。在 App \ Services 文件夹中,您将找到Laravel oem用户模型的注册商服务。
让我们用不同的案例来检查问题。
但首先,基本......
让我们假设所有逻辑都应放在控制器内。
RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store()
{
$data = Request::all(); //requested data via Facade
//prepare validatation
$validation = Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
//validate
if ($validation->fails())
{
return redirect()->back()->withErrors($v->errors());
}
// create the client
Client::create([
'fullname' => Request::input('fullname'),
'email' => Request::input('email'),
]);
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
第二个解决方案
您可能愿意分离验证逻辑并应用一些依赖注入。
RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
// create the client
Client::create([
'fullname' => $request->input('fullname'),
'email' => $request->input('email'),
]);
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
ClientFormRequest.php
use Stringy\create;
use App\User;
use Validator;
use App\Http\Requests\ClientFormRequest;
class ClientFormRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users'
];
}
}
第三种解决方案
您可能愿意更进一步,甚至将对象创建逻辑分离为服务,以便在任何地方使用它。现在您的请求文件将保持不变。然而,
RegisterController.php
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
use App\Services\ClientRegistrar;
class RegisterController extends Controller {
private $registrar;
public function __construct(ClientRegistrar $registrarService)
{
$this->registrar = $registrarService;
}
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
$newClient = $this->registrar->create($request->all());
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!')->compact('newClient');
}
}
应用\服务\ ClientRegistrar.php
use App\Client;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
class ClientRegistrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}
/**
* Create a new client instance after a valid registration.
*
* @param array $data
* @return Client
*/
public function create(array $data)
{
// create the client
return Client::create([
'fullname' => $data['fullname'],
'email' => $data['email'],
]);
}
}
结论 没有正确和最好的方法来解决问题。为您和您的项目规模保持最佳适用和适当的方式。
您可能也感兴趣;
答案 1 :(得分:0)
错误消息告诉您正在覆盖 ClientFormRequest 类中的class ClientFormRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
}
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}
}
方法。所以删除那里的方法。而是在Controller中创建新客户端。
下面我更新了您的课程以反映更改。
<强> ClientFormRequest 强>
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
// ClientFormRequest was valid
// create the client
Client::create([
'fullname' => $request->input('fullname'),
'email' => $request->input('email'),
]);
return Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
<强> RegisterController 强>
{{1}}