一切看起来都对我好。 这很简单,但我不知道。 我到处都看。
问题:它没有重定向。 它没有给出错误没有任何反应。 但是当我进入浏览器http://site.dev/fail它会在屏幕上显示“失败”字样(因此它可以正常工作)。
routes.php文件:
Route::post('getir' , 'Ahir\Ticket\Controllers\TicketController@postInsert'); Route::get('fail', function() { return 'fail'; }); Route::get('success', function() { return 'success'; });编辑一切
场景:在site.dev/ (主页)我按提交表单有这个。
form action="getir" method="POST" role="form"所以按钮重定向我
Route::post('getir' , 'Ahir\Ticket\Controllers\TicketController@postInsert');所以这个postInsert在控制器票据下面触发。
控制器票:
<?php namespace Ahir\Ticket\Controllers; use BaseController, Input; //use Ahir\Ticket\Repositories\TicketInterface; use Ahir\Ticket\Adapters\AdapterInterface ; class TicketController extends BaseController { public function __construct(AdapterInterface $adapter) //TicketInterface $repository { //$this->repository = $repository; $this->adapter = $adapter; } public function postInsert() { $this->adapter->postInsert(); } }然后它来到这里
代码
public function postInsert() { // create the validation rules ------------------------ $rules = array( 'title' => 'required', 'content' => 'required', ); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { // i added here return vardump('fail'); it displays on screen. // so i know that program comes here // but the redirect below neither gives error nor redirect. //nothing happens here. idk why! return Redirect::to('fail')->withErrors($validator); } else { // validation successful --------------------------- $this->obj->insert([ 'title' => Input::get('title') , 'content' => Input::get('content') ]); //here DOESNT work too. return Redirect::to('success'); }Everything looks right to me. It's so simple, but I dont know. I've looked everywhere.
Problem: It doesn't redirect. It doesn't give error nothing happens. But when I enter the browser http://site.dev/fail it shows "fail" word on screen (so it works).
routes.php:
Route::post('getir' , 'Ahir\Ticket\Controllers\TicketController@postInsert'); Route::get('fail', function() { return 'fail'; }); Route::get('success', function() { return 'success'; });edit everything
Scenario: on site.dev/ (homepage) I press submit that form has this.
form action="getir" method="POST" role="form"so button redirect me to
Route::post('getir' , 'Ahir\Ticket\Controllers\TicketController@postInsert');so this postInsert is triggered below at controller ticket.
controller ticket:
<?php namespace Ahir\Ticket\Controllers; use BaseController, Input; //use Ahir\Ticket\Repositories\TicketInterface; use Ahir\Ticket\Adapters\AdapterInterface ; class TicketController extends BaseController { public function __construct(AdapterInterface $adapter) //TicketInterface $repository { //$this->repository = $repository; $this->adapter = $adapter; } public function postInsert() { $this->adapter->postInsert(); } }then it comes here
codes
public function postInsert() { // create the validation rules ------------------------ $rules = array( 'title' => 'required', 'content' => 'required', ); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { // i added here return vardump('fail'); it displays on screen. // so i know that program comes here // but the redirect below neither gives error nor redirect. //nothing happens here. idk why! return Redirect::to('fail')->withErrors($validator); } else { // validation successful --------------------------- $this->obj->insert([ 'title' => Input::get('title') , 'content' => Input::get('content') ]); //here DOESNT work too. return Redirect::to('success'); }最满意答案
问题是你没有从调用函数返回任何东西。
您的应用程序调用postInsert()单控制器上的postInsert()方法。 此函数调用另一个返回Redirect函数。
但是您没有将返回的Redirect传递回应用程序,因此postInsert()函数只是终止而没有任何输出。 应用程序不知道postInsert()函数中发生了什么,它只是等待返回的东西。 由于没有返回任何内容,HTTP响应只是空的。 为了将Redirect传递回应用程序,您还必须从调用函数返回它:
public function postInsert() { return $this->adapter->postInsert(); }The problem is that you don't return anything from the calling function.
Your application calls the postInsert() method on your ticket controller. This function calls another function which returns a Redirect.
But you don't pass that returned Redirect back to the application, so the postInsert() function just terminates without any output. The application doesn't know what happens within the postInsert() function, it just waits for something to be returned. And since nothing is returned, the HTTP response is simply empty. In order to pass that Redirect back to the application, you also have to return it from the calling function:
public function postInsert() { return $this->adapter->postInsert(); }更多推荐
发布评论