Home>

I'm making something like a bulletin board with Laravel.
When POST is performed, the following error is displayed.

Error message
The page has expired due to inactivity.
Please refresh and try again
Applicable source code
@ extends ('layouts.app')
@section ('content')
    <h1>New message creation page</h1>
    {!! Form :: model ($message, ['route' =>'messages.store']) !!}
        {{csrf_field ()}}
        {!! Form :: label ('content', 'message:') !!}
        {!! Form :: text ('content') !!}
        {!! Form :: submit ('post') !!}
    {!! Form :: close () !!}
@endsection

■ Generated HTML

(Omitted)
<html>
    <head>
        <meta charset = "utf-8">
        <meta http-equiv = "X-UA-Compatible" content = "IE = edge">
        <meta name = "viewport" content = "width = device-width, initial-scale = 1">
        <title>Title</title>
         <!-Boostrap->
        <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    </head>
    <body>
    <h1>New message creation page</h1>
    <form method = "POST" action = "http://9fbe8dd6a4e54ddb8742a5d6326e9408.vfs.cloud9.us-east-2.amazonaws.com/messages?" accept-charset = "UTF-8"><input name = "_token" type = "hidden" value = "tnJghx8bXaIITjnjcLIglYCChKGU8UDvaLkPyxIe">
        <input type = "hidden" name = "_ token" value = "tnJghx8bXaIITjnjcLIglYCChKGU8UDvaLkPyxIe">
        @csrf
        <label for = "content">Message:</label>
        <input name = "content" type = "text">
        <input type = "submit" value = "post">
    </form>
(Omitted)
</script>
</body>
</html>
Tried

https://qiita.com/marcon/items/da59ca5e289a979f8221

Looking here, there was no {{csrf_field ()}}, so I added it, but nothing changed.

Supplemental information (FW/tool version etc.)

Laravel5.5
Cloud9

  • Answer # 1

    {!! Form :: model ($message, ['route' =>'messages.store']) !!}
            @csrf
            {!! Form :: label ('content', 'message:') !!}
            {!! Form :: text ('content') !!}
            {!! Form :: submit ('post') !!}
        {!! Form :: close () !!}
    Is it the same if you use@csrflike

    ?

    Also,routes/web.phpdefinesmessages.storeor
    Also check that you have created a controller action that corresponds to the routing.

    Append
    {!! Form :: model ($message, ['route' =>['messages.store', $message]]) !!}
    What about rewriting

    ?

    Append 2

    Because I don't usually use form helpers in my work, I tried to find out if the answer was wrong.

    welcome.blade.php

    {!! Form :: model ($message, ['route' =>'messages.store']) !!}
                {!! Form :: label ('content', 'message:') !!}
                {!! Form :: text ('content') !!}
                {!! Form :: submit ('post') !!}
            {!! Form :: close () !!}

    web.php

    Route :: get ('/', function () {
        $message = new App \ Message ();
        return view ('welcome', ['message' =>$message]);
    });
    Route :: resource ('messages', 'MessagesController');

    MessagesController.php

    public function store () {
            return 'success';
        }

    As a result,successwas able to be displayed normally on the screen.
    When using the form helper,{{csrc_field ()}}seems to be unnecessary because it is automatically assigned.

    I don't know why I can't ...
    Stop the server for the time being

    composer dump-autoload
    composer update
    After performing

    , please reload the browser and check the operation again.