Doubts about requests and Returns

Asked

Viewed 35 times

1

I have 2 different pages but access the same control. The idea is that I recover which page made the request, because depending on what it takes the two different returns.

Ex.

I have a button to send a product to the cart, but this one button is on index (main page) and the other is in the product description. In case I click the button of the index, it should redirect me to own index but incrementing the item to cart, if I am in the product details it should redirect me to product details.

  1. The Laravel facilitates these types of requests, where multiple pages access the same controller and the request should be made to page that made the request as in the example above ?

1 answer

1


In fact it is a set of factors at first the routes that are configured with the type of verb (POST, GET, DELETE, PUT), example:

Route::get('/exemplo', "ExemploController@index");
Route::post('/exemplo', "ExemploController@index");

that means the address /exemplo accepts requests configured in the route file verb POST and GET and in the for classe Request I can define various behaviors within an action (Action) of controller, example:

class ExemploController extends Controller
{
    public function index(Request $request)
    {
        //dependendo do verb eu faço determinada operação.
        if ($request->isMethod('get'))
        {
            return view('empresafuncao');
        }
        if ($request->isMethod('post'))
        { 
            return 'post';
        }
    }
}

The Laravel can work that way?

R. Can, but, I wouldn’t recommend, for several reasons, one of them and the amount of code that solves several problems by breaking a Object Orientation which is sole responsibility, where a party solves a particular problem, in the case example presented, is already solving two, and may even get worse if this code has to be changed and grow. Maintenance with this becomes difficult and critical at various times of coding.


The facilitates these types of requests, where multiple pages access the same controller and the request should be made to page that made the request as in the example above?

R. Yes, it is very easy to work with requests and together with the routes take profits and various moments, the relationship of the request is that, can make the request to the same page, but it is not recommended as already explained in that reply.

Ideally, each request has its own Action/Controller and its associated type of request (verb), where the process is very clear to solve a particular problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.