Form submit intercepted by auth middleware in laravel 5











up vote
1
down vote

favorite












I 've been working on a laravel 5.7 blog project. I want to comment an article. I need to achieve this:




  • Before logging in, I can type anything in comment textarea.


  • I submit comment(of course it would be intercepted by auth
    middleware),


  • then I'm redirected to login page,


  • After logging in, **I hope my app could submit previous form
    data(or comment) automaticlly instead of typing the same comment again.


  • I think that's a very common bussiness logic in many websites nowdays,
    but how am I supposed to achieve this?


My comments controller here:



public function __construct()
{
$this->middleware('auth');
}

public function store(Post $post){
$comment = Comment::create([
'body' => session('comment')?:request('body'),
'post_id' => $post->id,
'user_id' => auth()->user()->id
//before logging in, you don't have an user_id yet.
]);
return back()->with('success', 'Add comment succeeded');
}


web.php Route here:



Route::post('/posts/{post}/comments', 'CommentsController@store')->name('addComment');


Basiclly auth middleware intercepted my form data submit, I want to go across the auth middleware with my form data. Not lost them after logging in.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I 've been working on a laravel 5.7 blog project. I want to comment an article. I need to achieve this:




    • Before logging in, I can type anything in comment textarea.


    • I submit comment(of course it would be intercepted by auth
      middleware),


    • then I'm redirected to login page,


    • After logging in, **I hope my app could submit previous form
      data(or comment) automaticlly instead of typing the same comment again.


    • I think that's a very common bussiness logic in many websites nowdays,
      but how am I supposed to achieve this?


    My comments controller here:



    public function __construct()
    {
    $this->middleware('auth');
    }

    public function store(Post $post){
    $comment = Comment::create([
    'body' => session('comment')?:request('body'),
    'post_id' => $post->id,
    'user_id' => auth()->user()->id
    //before logging in, you don't have an user_id yet.
    ]);
    return back()->with('success', 'Add comment succeeded');
    }


    web.php Route here:



    Route::post('/posts/{post}/comments', 'CommentsController@store')->name('addComment');


    Basiclly auth middleware intercepted my form data submit, I want to go across the auth middleware with my form data. Not lost them after logging in.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I 've been working on a laravel 5.7 blog project. I want to comment an article. I need to achieve this:




      • Before logging in, I can type anything in comment textarea.


      • I submit comment(of course it would be intercepted by auth
        middleware),


      • then I'm redirected to login page,


      • After logging in, **I hope my app could submit previous form
        data(or comment) automaticlly instead of typing the same comment again.


      • I think that's a very common bussiness logic in many websites nowdays,
        but how am I supposed to achieve this?


      My comments controller here:



      public function __construct()
      {
      $this->middleware('auth');
      }

      public function store(Post $post){
      $comment = Comment::create([
      'body' => session('comment')?:request('body'),
      'post_id' => $post->id,
      'user_id' => auth()->user()->id
      //before logging in, you don't have an user_id yet.
      ]);
      return back()->with('success', 'Add comment succeeded');
      }


      web.php Route here:



      Route::post('/posts/{post}/comments', 'CommentsController@store')->name('addComment');


      Basiclly auth middleware intercepted my form data submit, I want to go across the auth middleware with my form data. Not lost them after logging in.










      share|improve this question













      I 've been working on a laravel 5.7 blog project. I want to comment an article. I need to achieve this:




      • Before logging in, I can type anything in comment textarea.


      • I submit comment(of course it would be intercepted by auth
        middleware),


      • then I'm redirected to login page,


      • After logging in, **I hope my app could submit previous form
        data(or comment) automaticlly instead of typing the same comment again.


      • I think that's a very common bussiness logic in many websites nowdays,
        but how am I supposed to achieve this?


      My comments controller here:



      public function __construct()
      {
      $this->middleware('auth');
      }

      public function store(Post $post){
      $comment = Comment::create([
      'body' => session('comment')?:request('body'),
      'post_id' => $post->id,
      'user_id' => auth()->user()->id
      //before logging in, you don't have an user_id yet.
      ]);
      return back()->with('success', 'Add comment succeeded');
      }


      web.php Route here:



      Route::post('/posts/{post}/comments', 'CommentsController@store')->name('addComment');


      Basiclly auth middleware intercepted my form data submit, I want to go across the auth middleware with my form data. Not lost them after logging in.







      laravel laravel-5 laravel-middleware






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 15:19









      Wayne lee

      612




      612
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          I think the solution to your problem is here:
          https://laravel.com/docs/5.7/session#storing-data






          share|improve this answer





















          • You are right, and i knew it. I just don't know how until yesterday.
            – Wayne lee
            Nov 12 at 22:24


















          up vote
          0
          down vote



          accepted










          Here is the solution.A little tricky.Save comment to the session first before go to auth middleware.After logging in, GET that route to create comment.
          Route:



          Route::get('/posts/{post}/comments', 'CommentsController@store')->name('addComment');
          Route::post('/posts/{post}/comments', 'CommentsController@commentSave');


          Comments controller:



          public function __construct()
          {
          $this->middleware('auth', ['except' => ['commentSave']]);
          }
          public function commentSave(Request $request){
          $url = URL::previous();
          session(['comment' => $request->input('body')]);
          return redirect("$url/comments");
          }

          public function store(Post $post){
          if(session('comment')){
          $comment = Comment::create([
          'body' => session('comment'),
          'post_id' => $post->id,
          'user_id' => auth()->user()->id
          ]);
          session(['comment' => null]);
          return redirect("posts/$post->id")->with('success', 'Add comment succeeded');
          }
          return redirect("posts/$post->id");
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250146%2fform-submit-intercepted-by-auth-middleware-in-laravel-5%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I think the solution to your problem is here:
            https://laravel.com/docs/5.7/session#storing-data






            share|improve this answer





















            • You are right, and i knew it. I just don't know how until yesterday.
              – Wayne lee
              Nov 12 at 22:24















            up vote
            0
            down vote













            I think the solution to your problem is here:
            https://laravel.com/docs/5.7/session#storing-data






            share|improve this answer





















            • You are right, and i knew it. I just don't know how until yesterday.
              – Wayne lee
              Nov 12 at 22:24













            up vote
            0
            down vote










            up vote
            0
            down vote









            I think the solution to your problem is here:
            https://laravel.com/docs/5.7/session#storing-data






            share|improve this answer












            I think the solution to your problem is here:
            https://laravel.com/docs/5.7/session#storing-data







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 17:09









            Youss

            304




            304












            • You are right, and i knew it. I just don't know how until yesterday.
              – Wayne lee
              Nov 12 at 22:24


















            • You are right, and i knew it. I just don't know how until yesterday.
              – Wayne lee
              Nov 12 at 22:24
















            You are right, and i knew it. I just don't know how until yesterday.
            – Wayne lee
            Nov 12 at 22:24




            You are right, and i knew it. I just don't know how until yesterday.
            – Wayne lee
            Nov 12 at 22:24












            up vote
            0
            down vote



            accepted










            Here is the solution.A little tricky.Save comment to the session first before go to auth middleware.After logging in, GET that route to create comment.
            Route:



            Route::get('/posts/{post}/comments', 'CommentsController@store')->name('addComment');
            Route::post('/posts/{post}/comments', 'CommentsController@commentSave');


            Comments controller:



            public function __construct()
            {
            $this->middleware('auth', ['except' => ['commentSave']]);
            }
            public function commentSave(Request $request){
            $url = URL::previous();
            session(['comment' => $request->input('body')]);
            return redirect("$url/comments");
            }

            public function store(Post $post){
            if(session('comment')){
            $comment = Comment::create([
            'body' => session('comment'),
            'post_id' => $post->id,
            'user_id' => auth()->user()->id
            ]);
            session(['comment' => null]);
            return redirect("posts/$post->id")->with('success', 'Add comment succeeded');
            }
            return redirect("posts/$post->id");
            }





            share|improve this answer

























              up vote
              0
              down vote



              accepted










              Here is the solution.A little tricky.Save comment to the session first before go to auth middleware.After logging in, GET that route to create comment.
              Route:



              Route::get('/posts/{post}/comments', 'CommentsController@store')->name('addComment');
              Route::post('/posts/{post}/comments', 'CommentsController@commentSave');


              Comments controller:



              public function __construct()
              {
              $this->middleware('auth', ['except' => ['commentSave']]);
              }
              public function commentSave(Request $request){
              $url = URL::previous();
              session(['comment' => $request->input('body')]);
              return redirect("$url/comments");
              }

              public function store(Post $post){
              if(session('comment')){
              $comment = Comment::create([
              'body' => session('comment'),
              'post_id' => $post->id,
              'user_id' => auth()->user()->id
              ]);
              session(['comment' => null]);
              return redirect("posts/$post->id")->with('success', 'Add comment succeeded');
              }
              return redirect("posts/$post->id");
              }





              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Here is the solution.A little tricky.Save comment to the session first before go to auth middleware.After logging in, GET that route to create comment.
                Route:



                Route::get('/posts/{post}/comments', 'CommentsController@store')->name('addComment');
                Route::post('/posts/{post}/comments', 'CommentsController@commentSave');


                Comments controller:



                public function __construct()
                {
                $this->middleware('auth', ['except' => ['commentSave']]);
                }
                public function commentSave(Request $request){
                $url = URL::previous();
                session(['comment' => $request->input('body')]);
                return redirect("$url/comments");
                }

                public function store(Post $post){
                if(session('comment')){
                $comment = Comment::create([
                'body' => session('comment'),
                'post_id' => $post->id,
                'user_id' => auth()->user()->id
                ]);
                session(['comment' => null]);
                return redirect("posts/$post->id")->with('success', 'Add comment succeeded');
                }
                return redirect("posts/$post->id");
                }





                share|improve this answer












                Here is the solution.A little tricky.Save comment to the session first before go to auth middleware.After logging in, GET that route to create comment.
                Route:



                Route::get('/posts/{post}/comments', 'CommentsController@store')->name('addComment');
                Route::post('/posts/{post}/comments', 'CommentsController@commentSave');


                Comments controller:



                public function __construct()
                {
                $this->middleware('auth', ['except' => ['commentSave']]);
                }
                public function commentSave(Request $request){
                $url = URL::previous();
                session(['comment' => $request->input('body')]);
                return redirect("$url/comments");
                }

                public function store(Post $post){
                if(session('comment')){
                $comment = Comment::create([
                'body' => session('comment'),
                'post_id' => $post->id,
                'user_id' => auth()->user()->id
                ]);
                session(['comment' => null]);
                return redirect("posts/$post->id")->with('success', 'Add comment succeeded');
                }
                return redirect("posts/$post->id");
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 22:36









                Wayne lee

                612




                612






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250146%2fform-submit-intercepted-by-auth-middleware-in-laravel-5%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine