路线问题:URL不会更改

时间:2015-03-08 14:04:11

标签: laravel laravel-4 routes

我创建了一个编辑邮件的页面,我可以编辑我的邮件并保存在我的数据库中。保存后,我return View::make("messages.index");

但如果我查看网址,则为:http://localhost:8000/update/42而不是http://localhost:8000/messages

我做错了什么吗? (我假设我在routes.php中犯了一个错误,但我找不到它)

路线:

Route::get('/', array('as' => '/', function()
{
    return View::make('index');
}));

Route::get("Messages", function()
{
    return View::make("Messages.index");
});

Route::resource('messages', 'MessageController');
Route::post('messages/{messages}', 'MessageController@destroy');
Route::put('update/{messages}', array('as' => 'putMessage', 'uses' => 'MessageController@update'));
Route::get('messages/{messages}/edit', array('as' => 'editMessage', 'uses' => 'MessageController@edit'));

Route::post('messages', array('as' => 'storeMessage', 'uses' => 'MessageController@store'));

MessageController:

<?php

class MessageController extends \BaseController {


    public function index()
    {
        $messages = Message::join('tblMessages_tblTeams', 'tblMessages.PK_message', '=', 'tblMessages_tblTeams.FK_message')
            ->join('tblTeams', 'tblMessages_tblTeams.FK_team', '=', 'tblTeams.PK_team')
            ->where('PK_team', 1)
            ->orderBy('created_at', 'DESC')
            ->get();

        return View::make("messages.index")->withMessages($messages); 
    }


    public function create()
    {
        //
    }

    // HARD CODED WILL BE REPLACED WHEN ABLE TO LOG IN
    // FK_user => Auth::id()->FK_team 
    public function store()
    {
        $date = new DateTime;

        // INSERT IN DATABASE
        $message = new Message;
        $message->FK_user = 1;
        $message->priority = Input::get("messagePriority");
        $message->title = Input::get('messageTitle');
        $message->content = Input::get('messageContent');
        $message->visible = true;
        $message->showFrom = Input::get('messageShowFrom');
        $message->removeFrom = Input::get('messageHideFrom');
        $message->created_at = $date;
        $message->updated_at = $date;
        $message->save();

        $message->teams()->attach(array(1,3));

        return Redirect::back();
    }


    public function show($id)
    {
        $deleteMessage = Message::findOrFail($id); 
        $deleteMessage->visible = 0;
        $deleteMessage->save();

        return Redirect::back();
    }


    public function edit($id)
    {
        $message = Message::findOrFail($id); 
        $showFrom = $message->showFrom;
        $removeFrom = $message->removeFrom;
        $showFrom = str_replace(' 00:00:00', '', $showFrom);
        $removeFrom = str_replace(' 00:00:00', '', $removeFrom);
        return View::make('messages.index')
            ->withMessage($message)
            ->with("showFrom", $showFrom)
            ->with("removeFrom", $removeFrom);
    }


    public function update($id)
    {   
        $date = new DateTime;
        $editMessage = Message::findOrFail($id); 

        $editMessage->FK_user = 1;
        $editMessage->priority = Input::get("messagePriority");
        $editMessage->title = Input::get('messageTitle');
        $editMessage->content = Input::get('messageContent');
        $editMessage->visible = true;
        $editMessage->showFrom = Input::get('messageShowFrom');
        $editMessage->removeFrom = Input::get('messageHideFrom');
        $editMessage->updated_at = $date;
        $editMessage->save();

        return View::make("messages.index");    
    }

    public function delete($id)
    {
        //   
    }

    public function destroy($id)
    {
        dd('destroy');
    }

}

2 个答案:

答案 0 :(得分:2)

您在这里返回的是页面/app/views/Messages/index.php

return View::make("Messages.index");

如果您想要拨打其他路线,请使用:

Redirect::route('messages');

答案 1 :(得分:1)

这就是它的工作方式,因为您的表单已提交给update/42。 您可以通过重定向返回,以获得您希望用户所在的URL。

return Redirect::to('/Messages');

如果您想将任何值带到该视图,您可以使用 ->withInput();

另请参阅this了解更多信息