Nahid的消息传递包

时间:2017-01-10 13:11:36

标签: laravel-5

这是我的MessageController.php文件

    class MessageController extends Controller
{
    protected $authUser;
    public function __construct()
    {
        $this->middleware('auth');
        Talk::setAuthUserId(Auth::user()->id);

        View::composer('partials.peoplelist', function($view) {
            $threads = Talk::threads();
            $view->with(compact('threads'));
        });
    }

    public function chatHistory($id)
    {
        $conversations = Talk::getMessagesByUserId($id);
        $user = '';
        $messages = [];
        if(!$conversations) {
            $user = User::find($id);
        } else {
            $user = $conversations->withUser;
            $messages = $conversations->messages;
        }

        return view('messages.conversations', compact('messages', 'user'));
    }

    public function ajaxSendMessage(Request $request)
    {
        if ($request->ajax()) {
            $rules = [
                'message-data'=>'required',
                '_id'=>'required'
            ];

            $this->validate($request, $rules);

            $body = $request->input('message-data');
            $userId = $request->input('_id');

            if ($message = Talk::sendMessageByUserId($userId, $body)) {
                $html = view('ajax.newMessageHtml', compact('message'))->render();
                return view('messages.conversations', compact('messages', 'user'));
            }
        }
    }

    public function ajaxDeleteMessage(Request $request, $id)
    {
        if ($request->ajax()) {
            if(Talk::deleteMessage($id)) {
                return response()->json(['status'=>'success'], 200);
            }

            return response()->json(['status'=>'errors', 'msg'=>'something went wrong'], 401);
        }
    }

我正在尝试从此表单发送消息

<form action="{{url('/message_send')}}" method="post" id="talkSendMessage">
    <textarea name="message-data" id="message-data" placeholder ="Type your message" rows="3"></textarea>
    <input type="hidden" name="_id" value="{{@request()->route('id')}}">
    <button type="submit">Send</button>
</form>

但它不起作用,在RouteCollection.php第161行中出现 NotFoundHttpException错误,这是我的routes.php文件

Route::get('message/{id}', 'MessageController@chatHistory')->name('message.read');

Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
   Route::post('message_send', 'MessageController@ajaxSendMessage')->name('message.new');
   Route::delete('message/delete/{id}', 'MessageController@ajaxDeleteMessage')->name('message.delete');
});

我不明白错误来自哪里?

1 个答案:

答案 0 :(得分:1)

您错过了在route group中定义的前缀。

Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {

您还必须在表单的操作

中添加前缀
<form action="{{url('/ajax/message_send')}}" method="post" id="talkSendMessage">