localhost页面在laravel中不起作用

时间:2016-05-19 03:52:05

标签: laravel

这是我的控制器代码:

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route; 
use Input; 
use Illuminate\Support\Facades\Redirect; 
use Illuminate\Http\Request; 
use App\Http\Requests; 
use App\models\Designation; 
use Validator;
class Cdesigination extends Controller
{
   public $flight;
    public function __construct(){
        $this->flight = new Designation; 
    }
    public function index(request $request)
    {   
        $this->flight->name = $request->name;
        $this->flight->detail = $request->detail;
        $this->flight->token_key = $request->_token;
        $data=$this->flight->save();
        if($data){
        return Redirect::to('posts')->withInput()->with('success', 'Inserted Successfully.');
        }
        else {
         return Redirect::to('posts')->withInput()->with('success', 'Not inserted Successfully.'); 
        }
     return view('designation');
    }
}

这是路线代码:

Route::get('/posts', 'Cdesigination@index');

麻烦在哪里以及如何解决?

我认为重定向关键字会造成麻烦,因为当我关注redirect:: to关键字然后正常工作时。

2 个答案:

答案 0 :(得分:0)

它不是重定向。您正在使用withInput()with('success', 'Inserted Successfully.')。使用任何一个。您可能想要删除withInput()并尝试。你也没有在你的页面上显示laravel错误吗?

答案 1 :(得分:0)

您似乎要/posts使用Cdesigination@index操作。此操作始终重定向到同一页面,并重定向到自身。它将一次又一次地重定向到自己。

所以永远不会加载view('designation')

if ($data) {
    // If $data is true, redirect
    return Redirect::to('posts')->withInput()->with('success', 'Inserted Successfully.');
} else {
    // If not, redirect too
    return Redirect::to('posts')->withInput()->with('success', 'Not inserted Successfully.'); 
}

// Any code here will never be executed

我不确定你想要完成什么,但是如果你想用消息加载designation视图,你应该这样做:

if ($data) {
    // If $data is true, redirect
    return view('designation')->withInput()->with('success', 'Inserted Successfully.');
} else {
    // If not, redirect too
    return view('designation')->withInput()->with('success', 'Not inserted Successfully.'); 
}