路由问题:URL 不变

问题描述 投票:0回答:2

我创建了一个页面来编辑消息,我可以编辑我的消息并将其保存在我的数据库中。保存后,我

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

但是如果我查看 URL,它是: 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'));

消息控制器:

<?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');
    }

}
laravel laravel-4 routes
2个回答
2
投票

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

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

如果你想调用其他路由,你应该使用:

Redirect::route('messages');

1
投票

这就是它的工作方式,因为您的表单已提交至

update/42
。 您可以通过像下面这样的重定向来返回您希望用户访问的 URL。

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

如果您想将任何值传递到该视图,您可以使用

->withInput();

另请参阅了解更多信息

© www.soinside.com 2019 - 2024. All rights reserved.