Domanda

I need to return a different view from a controller depending on the route that is requested.

For example: in my application I have clients, devices and campaigns. All have CRUD's created, but in some cases I want to do something like "view clients, delete his campaign and returning to the clients view" but my campaignsController@delete returns to campaigns by default.

The idea is not rewrite the same controller only changing the route of returning, does Laravel have something to help with this?

Thank you

È stato utile?

Soluzione

Laravel will not control the whole flow of your application. If you have a campaign delete router:

Route::delete('campaign/{id}');

and it returns to campaigns

class CampaignController extends Controller {

    public function delete($id)
    {
        $c = Campaign::find($id);

        $c->delete();

        return Redirect::route('campaigns');
    }

}

You will have to trick your route to make it go to wherever you need to, there should be dozens of ways of doing that, this is a very simple one:

Route::delete('campaign/{id}/{returnRoute?}');

class CampaignController extends Controller {

    public function delete($id, $returnRoute = null)
    {
        $returnRoute = $returnRoute ?: 'campaigns';

        $c = Campaign::find($id);

        $c->delete();

        return Redirect::route($returnRoute);
    }

}

And create the links in those pages by using the return route option:

link_to_route('campaign.delete', 'Delete Campaign', ['id' => $id, 'returnRoute' => 'clients'])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top