samedi 29 avril 2017

laravel 5; redirect to previous section after store or view

I think this is a basic problem or question, but i can't find any solution that solves this problem, so probably i'm searching in the wrong way or on the wrong keywords.

I have a bunch of articles in my DB, and there are a number of ways to read/edit them on the site. - The visitor can see them in a list, click on them and read the whole article - The registered users can see them in the same list, but click on them to read or edit the article - The admin can see a different list (an different view), and edit or delete them and i'm sure i will come up with some other ways to read or edit the articles or other data.

The point is, that if a visitor or user had read or edited the article, i want them to return to their own index of articles, but if an admin has read or edited the article i want him to go back to the admin-index.

So from the start i have a number of routes:

Route::get('/article'... will display the index of articles
Route::get('/article/{name}'... will display an article
Route::get('/article/{name}/edit'... will display the edit form for the article
Route::post('/article/{name}'... will update the article

And for the admin:

Route::get('/admin/article'... will display the index of article
Route::get('/admin/article/{name}'... will display an article
Route::get('/admin/article/{name}/edit'... will display the edit form for the article
Route::post('/admin/article/{name}'... will update the article

But..Route::post('/article/{name}' and Route::post('/admin/article/{name} should point to the same function to update the data, and that function should redirect to different routes, in this case Route::get('/article' and Route::get('/admin/article'

I really dont want to write 2 different routines to store or update the data in the database, that are in fact the same, except for the 'redirect'-line after updating or storing.

My first thought was to make 2 routes, with two entries in my controller that each call the same routine that saves the data, and return where i can redirect, but then i would have to use different 'actions' in my form. That would mean i would have to make 2 forms that are the same, except for the 'action'-line.

Whatever i do, i would have to redirect somewhere based on the section of my site where i was before i started to read or edit my data.

I read something about the 'back()'-function, but that won't help me because i want to be able to read an article, and then choose to edit it, then return back to my index.

I hope i was able to explain what i want to do, and i'm sure i'm not the only one looking for this, but again, i failed in finding a right solution.

What is het best way to achieve this?!?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2pIbVrI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire