简体   繁体   中英

How to detect where request is coming from?

Currently I have a Controller which controls the data sent and operation done.
For example, when I hit the home page, I would hit Route::get('home', 'Controller@select'); which will select existing data and show it at that page.
On the same page, I have provided a form which user can input data into it and press the submit button which will hit this one Route::post('home', 'Controller@insert'); , this works since it can differentiate between GET (from other page to home) and POST (from same page, but entered from the submit button form).

Now, I have the DELETE button on the very same page, but this one is that I'm using get, so if I still use Route::get('home', 'Controller@delete'); , it won't work, since it the Controller@select will override it.

My question is that how do I detect where the request is coming from so I can prepare different operation for different request from the user?
I have a basic PHP knowledge, and I kinda want it similar to like if(isset($_GET['delete'])) , do delete operation.

Another solution would be to move all the page to a different page, then redirect it to the home page but I still want to stick with the same page so it is convenient for the user to see the data flow easily.

You need to change the route for example if you use Route::get('home', 'Controller@select'); it's mind you are pointing to Controller->select() method when you go to the url home for delete you should try something like Route::get('home/delete', 'Controller@delete'); that mind when you go to the url home/delete will point to Controller->delete()

You can either add "?delete=1" and then check it in "select" method;

Route::post('delete', 'Controller@drop');

or you can make Route::delete('home', 'Controller@drop') ; But then you have to make delete request through ajax request like:

 $("#deleteButton").click(function() {
     $.ajax({
      method: "DELETE",
      url: "index.php",
     })
    });

When ever you are trying to perform delete operation you should set method as DELETE . However, by default we have only GET and POST methods. Laravel has nice wrap around( method_field() ) for this to work. You simply have to add _method field to the form.

Register Route

Route::destroy('home', 'Controller@destroy');

By HTML form

<form method="POST" action="home">
    {!! csrf_field() !!}
    {!! method_field('DELETE') !!}
</form>

By Ajax

$.ajax({
    url: "home",
    method: "POST",
    data : { '_method' : 'DELETE', '_token' : 'YOUR CSRF TOKEN HERE'}
})

I use a dummy page from the form action like action="insert", then at the Route: Route::post('insert', 'Controller@insert'); and at the Controller is just doing the operation and return back the view to ('home').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM