Laravel路由不适用于发布 [英] Laravel Routing does not work with post

查看:101
本文介绍了Laravel路由不适用于发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Laravel中使用RESTful路由,并且遇到以下问题.我的页面包括2个下拉菜单和一个提交按钮.首次加载页面时,Controller中的show()函数使用两个数据库查询的结果填充两个下拉列表. 在每个下拉菜单中选择一个值并单击提交"按钮后,我希望页面将另一个查询的结果显示为表格.

I'm trying to use RESTful Routing in Laravel and i'm encountering the following problem. My page consist of 2 Dropdowns, and a submit button. Upon loading the page for the first time, the show() function in Controller populates both of the dropdowns with results from 2 database queries. After i have selected a value in each of the dropdowns and hit the submit button, i would like the page to display the results of another query as a table.

我的show()方法可以正常工作,因为下拉列表已按需填充.我已经使用store()方法来处理帖子(点击提交后),但是它却给了我一个空白页面.

My show() method works, since the dropdowns are populated as they should. I have used the store() method to handle the post (after hitting the submit), but it gives me a blank page instead.

路线:

 Route::resource('web_tools', 'WebController');

控制器:

 class WebController extends BaseController {
       public function show() {
              $filters = WebToolsPage::QueryFilters();
              $category = WebToolsPage::QueryCategory(); 

              return View::make('top_pages.table',
              ['Filter'=>$filters, 'Category'=>$category]);
       }

       public function store() {
              $filt = Input::get('filt');
              $cat  = Input::get('cat');
              $filters = WebToolsPage::QueryFilters(); 
              $category = WebToolsPage::QueryCategory();
              $query = WebToolsPage::QueryTable($filt, $kat);
              return View::make('top_pages.table',
              ['Webmasters'=>$query,'Filter'=>$filters, 'Category'=>$category]);
       }

       public function index(){}
       public function create(){}
 }

查看:

 {{Form::open()}}

 {{ Form::select('filt', $Filter) }}  
 {{ Form::select('kat', $Kategorie) }}

 {{ Form::Submit('Filter') }}

 ......

 {{Form::close()}}

我的模型包含3个查询,其中2个用于下拉菜单,1个用于在第二个控制器方法中调用的表. 我还要提及的是,当我使用Route :: controller(...)时,一切都可以正常工作.而且由于我在控制器中缺少有关方法的错误,所以我继续定义这些方法并将其留空(?). 任何帮助将不胜感激.

My Model contains the 3 Queries, 2 for the dropdowns and 1 for table to be called within the second controller method. I would also like to mention that everything works perfectly when i use Route::controller(...). And because i was getting errors about methods missing in my controller, i just went ahead and defined these methods and left them empty (?). Any help would be greatly appreciated.

推荐答案

资源不应该在URL中具有方法名称.对/web_tools进行POST请求时,将调用store()方法.如果执行GET,将调用index().请参阅 Laravel文档中的表格:

A resource isn't supposed to have the method name in the URL. When you do a POST-request to /web_tools, the store() method is called. If you do a GET, index() will be called. Refer to the table in the Laravel documentation:

如果要使用显式命名,则必须将其注册为控制器,然后使用RESTful方法命名模式:

If you want the explicit naming you would have to register it as a controller instead and use the RESTful method naming pattern:

在您的routes.php中:

Route::controller('web_tools', 'WebController');

和您的WebController.php:

class WebController extends BaseController {
       public function getShow() {
              $filters = WebToolsPage::QueryFilters();
              $category = WebToolsPage::QueryCategory(); 

              return View::make('top_pages.table',
              ['Filter'=>$filters, 'Category'=>$category]);
       }

       public function postStore() {
              $filt = Input::get('filt');
              $cat  = Input::get('cat');
              $filters = WebToolsPage::QueryFilters(); 
              $category = WebToolsPage::QueryCategory();
              $query = WebToolsPage::QueryTable($filt, $kat);
              return View::make('top_pages.table',
              ['Webmasters'=>$query,'Filter'=>$filters, 'Category'=>$category]);
       }

       public function getIndex(){}
       public function getCreate(){}
 }

发布/重定向/获取模式

此外,您还应该真正遵循PRG(发布/重定向/获取)模式.在store()方法中,您真正想做的是:

The Post/Redirect/Get pattern

Also, you should really follow the PRG (Post/Redirect/Get) pattern. In your store() method, what you actually want to do is this:

       public function store() {
              $filt = Input::get('filt');
              $cat  = Input::get('cat');
             //Store whatever you want to store
              return Redirect::action(self::class."@index");
       }

一般来说,资源路由

首先,我认为您误解了资源丰富的控制器和路由.它们用于资源(即数据库中表示的内容;用户,帖子,评论等.),因此store方法实际上应该在存储库中持久存储(存储)新资源.数据库.让用户获取资源:

Resourceful routing in general

In the first place, I think you misunderstood resourceful controllers and routing. They're used for resources (i.e. something represented in a database; users, posts, comments, etc..), so the store-method should actually persist(store) a new resource in the database. Lets take a User for a resource:

  • 获取/用户::index():显示用户列表
  • 获取/user/create: create()显示一个表单,您在其中输入凭据以创建新用户(表单发布到/user)
  • POST/用户: store()将新用户保存在数据库中(如果遵循PRG模式,则会重定向!)
  • 获取/user/1::show(1):显示ID为1的个人资料的用户
  • 获取/user/1/edit::edit(1):显示一个表单,您可以在其中更改用户1的信息(表单发布到/user/1)
  • PUT/user/1: update(1) :( Laravel中具有隐藏字段的POST请求)更新ID为1(f.
  • 的用户
  • 删除/user/1: destroy(1) :( Larvel中具有隐藏字段的POST请求)删除ID为1的用户
  • GET /user: index(): Shows a list of users
  • GET /user/create: create() Shows a form where you enter your credentials to create a new user (form posts to /user)
  • POST /user: store() Saves the new user in a database (and redirects if you follow the PRG-pattern!)
  • GET /user/1: show(1): Shows user with an id of 1's profile
  • GET /user/1/edit: edit(1): Shows a form where you can change user 1's information (form posts to /user/1)
  • PUT /user/1: update(1): (A POST request with a hidden field in Laravel) Updates the user with an id of 1(f.
  • DELETE /user/1: destroy(1): (A POST request with a hidden field in Larvel) Deletes the user with an ID of 1

这篇关于Laravel路由不适用于发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆