【问题标题】:Store method in Laravel Controller 5.5 on server does not work服务器上 Laravel Controller 5.5 中的存储方法不起作用
【发布时间】:2018-06-23 23:42:54
【问题描述】:

我在 laravel 5.5 中开发了一个 CRUD 应用程序 所有方法在本地 laravel 中都可以正常工作(我的意思是使用命令 php artisa serve 启动的“模拟器”),但是在 xampp 中部署应用程序后,我的控制器中的方法存储停止工作(仅此而已)!

在我的 web.php 中,我添加了这条路线:

Route::resource('example','exampleControllerView');

php artisan 命令的结果:

            POST     | example                           | example.store        | App\Http\Controllers\exampleControllerView@store    
|        | GET|HEAD  | example                           | example.index        | App\Http\Controllers\exampleControllerView@index  
|        | GET|HEAD  | example/create                    | example.create       | App\Http\Controllers\exampleControllerView@create 
|        | GET|HEAD  | example/{example}                     | example.show         | App\Http\Controllers\exampleControllerView@show  
|        | PUT|PATCH | example/{example}                     | example.update       | App\Http\Controllers\exampleControllerView@update 
|        | DELETE    | example/{example}                     | example.destroy      | App\Http\Controllers\exampleControllerView@destroy
|        | GET|HEAD  | example/{example}/edit                | example.edit         | App\Http\Controllers\exampleControllerView@edit

这是我的控制器中存储的方法

public function store(Request $request)

{

$request->validate([
  'name' => 'required|min:4',
  'description'=> 'required',
]);

$example = example::create(['name' => $request->name,'description' => $request->description]);
return redirect('/example/'.$example->id);

}

这是视图创建(网址:http://localhost/project/public/example/create):

    <form action="/example" method="post">
{{ csrf_field() }}
 <div class="form-group">
   <label for="title">example nome</label>
   <input type="text" class="form-control" id="taskTitle"  name="name">
 </div>
 <div class="form-group">
   <label for="description">example descrizione</label>
   <input type="text" class="form-control" id="exampleDescription" name="description">
 </div>
 @if ($errors->any())
   <div class="alert alert-danger">
       <ul>
           @foreach ($errors->all() as $error)
               <li>{{ $error }}</li>
           @endforeach
       </ul>
   </div>
 @endif
 <button type="submit" class="btn btn-primary">Submit</button>

提交按钮后,我在新页面中看到这个 uri:

http://localhost/example

页面中出现 404 错误:找不到对象! 此服务器上不存在请求的 URL。

我认为这是路由和 url 的问题,但我不知道为什么在本地都可以正常工作...

提前感谢

【问题讨论】:

  • 请出示show控制器方法。
  • 你可以在上面看到,反正:
  • public function store(Request $request) { $request->validate([ 'name' => 'required|min:4', 'description'=> 'required', ]); $example = example::create(['name' => $request->name,'description' => $request->description]);返回重定向('/example/'.$example->id); }
  • show,不是store
  • 公共函数展示(example $example) { return view('example.show',compact('example',$example)); }

标签: laravel controller


【解决方案1】:

改变这一行-

 <form action="/example" method="post">

到这里-

 <form action="/project/public/example" method="post">

最好使用Laravel Collective,这将自动配置您的基本路径并有很多其他好处,例如csrf protection

【讨论】:

    【解决方案2】:

    您的代码应该可以正常工作。它将用户重定向到具有创建实体 ID 的 show 方法。尝试这样做,而不是使用模型绑定:

    public function show($id)
    {
        dd('The show method was executed and ID is ' . $id);
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 2020-04-30
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多