Laravel Post 控制器不工作(Symfony \ Component ...)

Posted

技术标签:

【中文标题】Laravel Post 控制器不工作(Symfony \\ Component ...)【英文标题】:Laravel Post Controller not working (Symfony \ Component...)Laravel Post 控制器不工作(Symfony \ Component ...) 【发布时间】:2019-01-15 16:56:14 【问题描述】:

我的整个 laravel 控制器无法正常工作。当我对此控制器 index() 发出 get 请求时,它工作得很好。但是当我向这个控制器发出一个 post 请求来存储()时,它不起作用。

当我尝试解决问题时,我开始注释掉代码或使用 dd()。然后很快注意到,当我注释掉我的整个控制器时,它对错误没有任何改变。 (或者当我 dd($user_id) 没有任何改变时)。

我的错误:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

路线文件:

<?php

Route::get('/', function () 
    return view('welcome');
);


Route::get('/test','TestController@index');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');
Route::get('/inspirations','InspirationsController@index')->middleware('auth');
Route::get('/spaces','SpacesController@index');
Route::get('/user/id','UserController@index'); // other profiles
Route::get('/user','UserController@myprofile'); // my profile
Route::get('/mymessages','MessagesController@index'); // messages


Route::get('/testauth/', function()

    var_dump(Auth::user()->id);
    // your code here
);

Route::post('/pins/inspiration_id/room_id','PinsController@store')->middleware('auth');
Route::post('/editRoom/id/name/description','RoomsController@update');
// how i was doing it --> Route::post('/sendmessage/receive_id/message','MessagesController@store');
Route::post('/sendmessage','MessagesController@store');


Auth::routes();

我的控制器:

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Models\Messages;
    use App\User;
    use Auth;

        class MessagesController extends Controller
        
            public function index()
            
                // We need to be able to see each user that has corresponded with this particular user. And only display them once on their users list.
                // Hence we made a 'correspondence_id' so we can filter that later on in vue.

                // Grab current user.
                $user_id = Auth::user()->id;

                // Grab all messages related to this user.
                $messages = Messages::where('send_id', $user_id)->orWhere('receive_id', $user_id)->get();

                foreach($messages as $message) 

                    // for each message we want to grab the first and last name of the person we received or send the message to.
                    if($user_id == $message['send_id']) 
                        // User_id is my id, so we don't want that name.
                     else 
                        // We want to grab their name.
                        $user = User::where('id', $message['send_id'])->first();

                        // Add this user to the message.
                        $message['firstname'] = $user['firstname'];
                        $message['lastname'] = $user['lastname'];
                        // Add profile_img url.
                        $message['profile_img'] = $user['profile_img'];
                        // Add id of user you are speaking to.
                        $message['correspondence_id'] = $message['send_id'];
                    

                    if($user_id == $message['receive_id']) 
                        // User_id is my id, so we don't want that name.
                     else 
                        // We want to grab their name.
                        $user = User::where('id', $message['receive_id'])->first();

                        // Add his first and last name to the message.
                        $message['firstname'] = $user['firstname'];
                        $message['lastname'] = $user['lastname'];

                        // This should have the image of the profile who is receiving the image (not the other user).
                        $currentUser = User::where('id', $message['send_id'])->first();
                        $message['profile_img'] = $currentUser['profile_img'];

                        // Add id of user speaking to you.
                        $message['correspondence_id'] = $message['receive_id'];

                    

                

                return compact('messages');

            
    public function store(Request $request)

    $receive_id = post('id');
    $message = post('message');

    // Grab current user.
    $user_id = Auth::user()->id;

    $messages = new Messages();

    $messages->fill($request->all());

    $messages->send_id = $user_id;

    $messages->receive_id = $receive_id;

    $messages->message = $message;

    $messages->save();

    $text = "Message stored";

    return compact("text");


错误:

我的 post 请求是通过 axios (vuex) 完成的:

sendMessage( commit , payload)
        var receive_id = payload.receive_id;
        var message = payload.message;
        console.log(payload)

        axios.post('/sendmessage/'+receive_id+'/'+message, 
        ).then(function (response) 
            console.log(commit);
            console.log("success");
        ).catch((response) => 
            // Get the errors given from the backend
            let errorobject = response.response.data.errors;
            for (let key in errorobject) 
                if (errorobject.hasOwnProperty(key)) 
                    console.log(errorobject[key]);
                    this.backenderror = errorobject[key];
                
            
        )
    

**发布请求的更改(由 Tschallacka 提出)**

sendMessage( commit , payload)
        var receive_id = payload.receive_id;
        var message = payload.message;
        console.log(payload)

        axios.post('/sendmessage',  receive_id: receive_id, message: message
        ).then(function (response) 
                console.log(commit);
                console.log("success");
            ).catch((response) => 
                // Get the errors given from the backend
                let errorobject = response.response.data.errors;
                for (let key in errorobject) 
                    if (errorobject.hasOwnProperty(key)) 
                        console.log(errorobject[key]);
                        this.backenderror = errorobject[key];
                    
                
            )

发布请求期间出错:

【问题讨论】:

你能展示一下你是如何发出帖子请求的 您为什么将发布请求视为获取请求? axios.post('/sendmessage/'+receive_id+'/'+message 发送receive_id 和消息作为post 参数axios.post('/sendmessage', id: receive_id, message: message ) 我并不是说它不起作用。我是说你滥用 post 方法作为 get 方法。只需将这些参数作为后变量发送。例如,这样您就不会达到 Internet Explorer 的 2048 个字符限制。 是的,错误的 url 将保留,您只是以更好的方式发送数据。此外,您现在必须获得 $receive_id = post('id'), $message = post('message') 而不是作为参数。在chrome中,打开网络面板,点击xhr,发送请求,点击红色请求,然后点击响应选项卡。并阅读响应,看看原始响应中是否有错误代码 哦,是的,对不起,把 laravel 和 octobercms 搞混了,你会想要 $request-&gt;input('variable_name'); 也你是 $messages-&gt;fill($request-&gt;all()); 然后手动填充对象。如果您没有保护所有内容,$request-&gt;all() 填充可能最终会设置不是数据库中的列的变量。 【参考方案1】:

不要将 POST 请求用作 GET 请求。您可能会遇到浏览器对 URL 长度的限制。

axios.post('/sendmessage/'+receive_id+'/'+message, 

进入

axios.post('/sendmessage',  id: receive_id, message: message )

然后在你的控制器中改变

public function store(Request $request,$receive_id, $message)

public function store(Request $request)

    $receive_id = $request->input('id');
    $message = $request->input('message');

要解决任何其他错误,请打开您的开发控制台。按 F12。 单击网络选项卡并选择 XHR 日志记录。

提出请求。它将显示为错误 500 请求。单击文件名(chrome 中的红色),然后单击响应。查看错误并进行诊断。

chrome 中的示例

你的情况

“消息”:“SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'updated_at'(SQL:插入消息'(receive_id,message,send_id,updated_at,created_at)值(3,测试, 1, 2018-08-08 13:00:54, 2018-08-08 13:00:54))"

$schema-&gt;timestamps() 添加到您的迁移文件或在您的消息模型中设置属性public $timestamps = false;

【讨论】:

以上是关于Laravel Post 控制器不工作(Symfony \ Component ...)的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 输入:get() 不工作

Laravel Yajra 不接受 post/get 查询请求

Laravel Route::apiResource Post 查询无法正常工作

Laravel 5 GET 正在工作,但 POST 方法不起作用

当方法更改为 GET 路由不像 POST 方法 Laravel 那样工作

Laravel 4:Input::all() 使用 $.ajax POST 不返回任何数据