search

Home  >  Q&A  >  body text

How to use csrf_token in non-views in Laravel

Excuse me, masters, how can I use csrf_token in Laravel without a view? I wrote a php file in public, and wrote a form in it that needs to be submitted by post, but csrf_token cannot be used. How to solve this problem?

習慣沉默習慣沉默2831 days ago542

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 16:52:07

    In blade, you can use {{csrf_token()}} to quickly get the csrf token value

    Track into the source code: app('session')->getToken() .

    From the above code, it should be inconvenient to use, because your own php file under public does not start laravel core resources through index.php, and the session must not be obtained. Laravel's session is processed and is not PHP native session

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 16:52:07

    1. Obtain through Session facade

    Session::token();

    2. Use app to create a session instance to obtain

    app('session')->getToken();

    3. Get it from blade

    {{csrf_token()}}

    reply
    0
  • Cancelreply