Redirecting the web client after deleting an item











up vote
1
down vote

favorite












In my web-app, it is possible to delete an Item when visiting any of 4 different pages:




  • the Show Item page

  • the Edit Item page

  • the List Items page

  • the List Items in Room page


If the visitor uses a delete button on either of the 2 List pages, they should remain on their current page when the deletion is complete. But if the visitor is on the Show or Edit pages, they should be redirected to the List Items in Room page when deletion is complete.



I have the following method in a Symfony3 controller, which uses the HTTP referer header:



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');
$rpath = parse_url($referer, PHP_URL_PATH );
$badreferrers = array(
$this->generateUrl('ci_item_show', ['id' => $item->getId()]),
$this->generateUrl('ci_item_edit', ['id' => $item->getId()])
);

if(in_array($rpath, $badreferrers) ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Would it be better to have different routes for the different use cases? Something like this?



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Route("/deletefromlist/{id}", name="ci_item_delete_from_list", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');

if($request->get('_route') == 'ci_item_delete' ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Or is there another better way altogether?










share|improve this question
























  • Please provide more context. Could you clarify the relationship between items and rooms?
    – 200_success
    Feb 24 '17 at 8:49










  • @200_success, item is many-to-one with room with item on the owning side.
    – dnagirl
    Feb 24 '17 at 14:36















up vote
1
down vote

favorite












In my web-app, it is possible to delete an Item when visiting any of 4 different pages:




  • the Show Item page

  • the Edit Item page

  • the List Items page

  • the List Items in Room page


If the visitor uses a delete button on either of the 2 List pages, they should remain on their current page when the deletion is complete. But if the visitor is on the Show or Edit pages, they should be redirected to the List Items in Room page when deletion is complete.



I have the following method in a Symfony3 controller, which uses the HTTP referer header:



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');
$rpath = parse_url($referer, PHP_URL_PATH );
$badreferrers = array(
$this->generateUrl('ci_item_show', ['id' => $item->getId()]),
$this->generateUrl('ci_item_edit', ['id' => $item->getId()])
);

if(in_array($rpath, $badreferrers) ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Would it be better to have different routes for the different use cases? Something like this?



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Route("/deletefromlist/{id}", name="ci_item_delete_from_list", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');

if($request->get('_route') == 'ci_item_delete' ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Or is there another better way altogether?










share|improve this question
























  • Please provide more context. Could you clarify the relationship between items and rooms?
    – 200_success
    Feb 24 '17 at 8:49










  • @200_success, item is many-to-one with room with item on the owning side.
    – dnagirl
    Feb 24 '17 at 14:36













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In my web-app, it is possible to delete an Item when visiting any of 4 different pages:




  • the Show Item page

  • the Edit Item page

  • the List Items page

  • the List Items in Room page


If the visitor uses a delete button on either of the 2 List pages, they should remain on their current page when the deletion is complete. But if the visitor is on the Show or Edit pages, they should be redirected to the List Items in Room page when deletion is complete.



I have the following method in a Symfony3 controller, which uses the HTTP referer header:



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');
$rpath = parse_url($referer, PHP_URL_PATH );
$badreferrers = array(
$this->generateUrl('ci_item_show', ['id' => $item->getId()]),
$this->generateUrl('ci_item_edit', ['id' => $item->getId()])
);

if(in_array($rpath, $badreferrers) ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Would it be better to have different routes for the different use cases? Something like this?



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Route("/deletefromlist/{id}", name="ci_item_delete_from_list", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');

if($request->get('_route') == 'ci_item_delete' ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Or is there another better way altogether?










share|improve this question















In my web-app, it is possible to delete an Item when visiting any of 4 different pages:




  • the Show Item page

  • the Edit Item page

  • the List Items page

  • the List Items in Room page


If the visitor uses a delete button on either of the 2 List pages, they should remain on their current page when the deletion is complete. But if the visitor is on the Show or Edit pages, they should be redirected to the List Items in Room page when deletion is complete.



I have the following method in a Symfony3 controller, which uses the HTTP referer header:



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');
$rpath = parse_url($referer, PHP_URL_PATH );
$badreferrers = array(
$this->generateUrl('ci_item_show', ['id' => $item->getId()]),
$this->generateUrl('ci_item_edit', ['id' => $item->getId()])
);

if(in_array($rpath, $badreferrers) ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Would it be better to have different routes for the different use cases? Something like this?



/**
* @Route("/delete/{id}", name="ci_item_delete", requirements={"id"="d+"}, options={"expose"=true})
* @Route("/deletefromlist/{id}", name="ci_item_delete_from_list", requirements={"id"="d+"}, options={"expose"=true})
* @Security("has_role('ROLE_USER') && is_granted('delete', item)")
* @param Request $request
* @param Item $item
* @return RedirectResponse
*/
public function deleteAction(Request $request, Item $item)
{
$referer = $request
->headers
->get('referer');

if($request->get('_route') == 'ci_item_delete' ){ //coming from a page that won't exist once the Item is deleted
$redirect = $this->generateUrl('ci_item_list_format_room', ['format_id'=> $item->getFormat()->getId(), 'room_id'=> $item->getRoom()->getId()]);
} else {
$redirect = $referer;
}

$em = $this->getDoctrine()->getManager();
$em->remove($item);
$em->flush();
$this->addFlash('success', 'msg.delete.item.success');

return $this->redirect($redirect);
}


Or is there another better way altogether?







php comparative-review url-routing symfony2 crud






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 24 '17 at 8:46









200_success

127k15148410




127k15148410










asked Jan 24 '17 at 23:21









dnagirl

11814




11814












  • Please provide more context. Could you clarify the relationship between items and rooms?
    – 200_success
    Feb 24 '17 at 8:49










  • @200_success, item is many-to-one with room with item on the owning side.
    – dnagirl
    Feb 24 '17 at 14:36


















  • Please provide more context. Could you clarify the relationship between items and rooms?
    – 200_success
    Feb 24 '17 at 8:49










  • @200_success, item is many-to-one with room with item on the owning side.
    – dnagirl
    Feb 24 '17 at 14:36
















Please provide more context. Could you clarify the relationship between items and rooms?
– 200_success
Feb 24 '17 at 8:49




Please provide more context. Could you clarify the relationship between items and rooms?
– 200_success
Feb 24 '17 at 8:49












@200_success, item is many-to-one with room with item on the owning side.
– dnagirl
Feb 24 '17 at 14:36




@200_success, item is many-to-one with room with item on the owning side.
– dnagirl
Feb 24 '17 at 14:36










1 Answer
1






active

oldest

votes

















up vote
0
down vote













There really is not much tangible difference between the two. I think I would not want to introduce a second route that does precisely the same thing as another route. I think it is sufficient to differentiate the redirect behavior based on the request properties.



In first, code example I think $badreferrers is a bad variable name. There is nothing bad about the referrer right? Those refererr values lead to a different redirect.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    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: "196"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fcodereview.stackexchange.com%2fquestions%2f153532%2fredirecting-the-web-client-after-deleting-an-item%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    There really is not much tangible difference between the two. I think I would not want to introduce a second route that does precisely the same thing as another route. I think it is sufficient to differentiate the redirect behavior based on the request properties.



    In first, code example I think $badreferrers is a bad variable name. There is nothing bad about the referrer right? Those refererr values lead to a different redirect.






    share|improve this answer

























      up vote
      0
      down vote













      There really is not much tangible difference between the two. I think I would not want to introduce a second route that does precisely the same thing as another route. I think it is sufficient to differentiate the redirect behavior based on the request properties.



      In first, code example I think $badreferrers is a bad variable name. There is nothing bad about the referrer right? Those refererr values lead to a different redirect.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        There really is not much tangible difference between the two. I think I would not want to introduce a second route that does precisely the same thing as another route. I think it is sufficient to differentiate the redirect behavior based on the request properties.



        In first, code example I think $badreferrers is a bad variable name. There is nothing bad about the referrer right? Those refererr values lead to a different redirect.






        share|improve this answer












        There really is not much tangible difference between the two. I think I would not want to introduce a second route that does precisely the same thing as another route. I think it is sufficient to differentiate the redirect behavior based on the request properties.



        In first, code example I think $badreferrers is a bad variable name. There is nothing bad about the referrer right? Those refererr values lead to a different redirect.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 25 '17 at 1:57









        Mike Brant

        8,713619




        8,713619






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f153532%2fredirecting-the-web-client-after-deleting-an-item%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

            Morgemoulin

            Scott Moir

            Souastre