Want clone the Message Page in new file name.
mynewmassagepage.blade.php
@extends('master.main')
@section('main-content')
@component('master.notification')
@slot('size')
col-md-8 col-md-offset-2
@endslot
@slot('title')
Send new message
@endslot
@if(session()->has('errormessage'))
<div class="alert alert-danger">
<strong>Whoops ! </strong><span>{{session()->get('errormessage')}}</span>
</div>
@endif
<form class="" action="{{route('sendmessagepost')}}" method="post">
<div class="form-group">
<label for="recipient">Recipient:</label>
<input type="text" name="recipient" id="recipient" value="@if($recipient !== null){{$recipient->username}}@endif" class="form-control">
</div>
<div class="form-group">
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="{{old('title')}}" class="form-control">
</div>
<div class="form-group">
<label for="text">Text:</label>
<textarea name="text" class="form-control" style="resize:none" rows="8" cols="80">{{old('text')}}</textarea>
</div>
<div class="form-group">
<center>
<button type="submit" name="button" class="btn btn-success">Send Message</button>
</center>
</div>
{{csrf_field()}}
</form>
@endcomponent
@stop
in web.php i use this code:
Route::get('mynewmassagepage}','MessageController@SendView')->name('sendmessage');
Route::post('mynewmassagepage','MessageController@SendPost')->name('sendmessagepost');
I always get the
MethodNotAllowedHttpException in RouteCollection.php line 233:
error back. I dont know what i am not make correct,its my first laravel project.
答案 0 :(得分:0)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\User;
use App\Message;
class MessageController extends Controller
{
public function SendView($username = null){
$user = Auth::user();
$recipient = User::where('username',$username)->first();
return view('message.send')->with([
'recipient' => $recipient
]);
}
public function SendPost(Request $request){
if ($request->recipient == null) {
session()->flash('errormessage','You must enter recipient\'s username');
return redirect()->back()->withInput();
}
if ($request->text == null) {
session()->flash('errormessage','You must send something');
return redirect()->back()->withInput();
}
if ($request->title == null) {
session()->flash('errormessage','You must enter title');
return redirect()->back()->withInput();
}
$recipient = User::where('username',$request->recipient)->first();
if ($recipient == null) {
session()->flash('errormessage','User with that username does not exist');
return redirect()->back()->withInput();
}
if ($recipient->id == Auth::user()->id) {
session()->flash('errormessage','You cannot send message to yourself');
return redirect()->back()->withInput();
}
$last_msg = Message::where('from',Auth::user()->id)->orderBy('created_at','desc')->first();
if ($last_msg !== null) {
$newTime = date("Y-m-d H:i:s",time());
if (Auth::user()->vendor == false) {
$last_time = date('Y-m-d H:i:s',strtotime('+1 minute',strtotime($last_msg->created_at)));
if ($newTime < $last_time) {
session()->flash('errormessage','You can send message every minute');
return redirect()->back()->withInput();
}
} else {
$last_time = date('Y-m-d H:i:s',strtotime('+10 seconds',strtotime($last_msg->created_at)));
if ($newTime < $last_time) {
session()->flash('errormessage','You can send message every ten seconds');
return redirect()->back()->withInput();
}
}
}
$m = new Message;
$m->uniqueid = 'ME'.str_random(28);
$m->to = $recipient->id;
$m->from = Auth::user()->id;
$m->title = $request->title;
$m->text = $request->text;
$m->save();
return redirect()->route('messages');
}
public function ViewReceived(){
$received = Message::where('to',Auth::user()->id)->orderBy('created_at','desc')->paginate(25);
return view('message.received')->with([
'received'=>$received
]);
}
public function ViewSent(){
$sent = Message::where('from',Auth::user()->id)->orderBy('created_at','desc')->paginate(25);
return view('message.sent')->with([
'sent'=>$sent
]);
}
public function ViewMessage($uniqueid){
$message = Message::where('uniqueid',$uniqueid)->first();
if ($message == null) {
return redirect()->route('messages');
}
if (Auth::user()->id !== $message->to && Auth::user()->id !== $message->from) {
return redirect()->route('messages');
}
if ($message->viewed == false && Auth::user()->id == $message->to) {
$message->viewed = true;
$message->save();
}
return view('message.view')->with([
'message'=>$message
]);
}
}
答案 1 :(得分:0)
异常意味着您可能向端点发送了错误类型的请求,例如GET到POST。或者Laravel错过了路线,因为名字相似。
尝试按照以下示例操作。
Route::post('sendmessage/post',[
'use'=>'MessageController@SendPost',
'as'=>'sendmessage.post'
]};
<form class="" action="{{route('sendmessage.post')}}" method="post">
<div class="form-group">
<label for="recipient">Recipient:</label>
<input type="text" name="recipient" id="recipient" value="@if($recipient !== null){{$recipient->username}}@endif" class="form-control">
</div>
<div class="form-group">
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="{{old('title')}}" class="form-control">
</div>
<div class="form-group">
<label for="text">Text:</label>
<textarea name="text" class="form-control" style="resize:none" rows="8" cols="80">{{old('text')}}</textarea>
</div>
<div class="form-group">
<center>
<button type="submit" name="button" class="btn btn-success">Send Message</button>
</center>
</div>
{{csrf_field()}}
</form>
答案 2 :(得分:0)
您的代码中存在打印错误,其中包含无关的}
。删除它,就像这样。
Route::get('mynewmassagepage','MessageController@SendView')->name('sendmessage');
答案 3 :(得分:0)
由于路线无效,您收到此错误。您的Route.php
文件中存在一些小错误,第233行。为什么您在}
路由中使用get
符号?删除这个额外的符号并使用它,如,
Route::get('/mynewmassagepage','MessageController@SendView')->name('sendmessage');