8

I would like to ask whether it's a good approach to redirect from within a model instead of a controller.

The reason I want to do that is because it is easier to unit test redirection from a model (I just pass a mock redirector object to the model in my tests) as opposed to controller which is more difficult to unit test. It also keeps controller thinner as all I do in the controller is create an instance of the model and pass it parameters from the request object. There is not a single if/else in the controller this way.

Is it a bad practise?

4

5 回答 5

4

Most often in webapplications - MVC or not - redirects are implemented on a high-level layer. In non OOP code this often is a high level global function that knows a lot about the global static state and what represents a request and a response therein.

In more OOP driven sites, you find this often as a method with the "response" object (compare Symfony2 and HTTP Fundamentals; note that Symfony2 is not MVC), however, it often then has also some similar methods (e.g. see Difference between $this->render and $this->redirect Symfony2).

As most often those "response" objects are central in the web-application this qualifies as well as a high-level layer in my eyes.

From a testing standpoint - especially with integration testing - you normally do not need to test for redirects specifically. You should test that generally your redirect-functionality works on the HTTP layer so that parts of your application that make use of it can rely on it. Common problems are to not really follow the suggestions given in the HTTP/1.1 specs like providing a response body with redirects. A well working webapplication should honor that. Same for using fully qualified URIs.

So how does all fit this into MVC here? In a HTTP context this could be simplified as the following:

  • The response is to tell the user to go somewhere else.
  • If the user would not be important, the application could forward directly - that is executing a different command, the client would not be used for that, the redirect not necessary (sub-command).
  • The response is to say: Done. And then: See next here this command (in form of an URI).

This pretty much sounds like that the actual redirect is just some output you send to the client on the protocol level in client communication. It belongs into the interface of that protocol you want to support. So not into the model but into the client-interface and the boundary of the client interface inside an MVC application is the controller AFAIK.

So the redirect probably is a view-value-object with a special meaning. To get that working in a HTTP MVC you need a full URL abstraction which most PHP frameworks and libraries make a big round around because it's not well known how that works. So in the end I'd say: Do as Symfony2 has done, place it in a high level layer component, drop MVC and live with the deficiencies.

Everything else is hard to achieve, if you try otherwise, there is high risk to not stop with abstracting anymore.

于 2013-05-21T13:37:16.680 回答
3

Neither controller nor model should be redirecting anything anywhere. HTTP Location header is form of a response, which strictly in purview of views.

Model layer deals with business logic, it should be completely oblivious even to the existence of presentation layer.

Basically, it goes down to this: controllers handle input, views handle output. HTTP headers are part of output.

Note: when dealing with Rails clones, it is common to see redirects performed in "controller". It is because what they call "controller" is actually a merger of view and a controller responsibilities. This is a side-effect of opting to replace real views with simple templates as the 3rd side of triad.

于 2013-05-21T12:31:46.917 回答
2

I would say yes, it is wrong. As far as I understood, while models manage data and views manage layouts (i.e. how data should be displayed), controllers are only and exclusively in charge of the HTTP requests/responses management (in the case of a web app), and redirections typically belong to that tier in my opinion.

Examples in common frameworks

Symfony:

return $this->redirect($this->generateUrl('homepage'));

Spring MVC:

return "redirect:/appointments";
于 2013-05-21T12:43:34.560 回答
0

I think that you could have a model for your applications work flow or navigation (in your model layer) and then have your controller translate the different concepts in your work flow/navigation model into what views are to be displayed.

Your work flow class/module could know about the different activities/phases/steps that are available to the user, and it model your application kind of like a state machine. So your controller would make calls to this module to update the state and would recieve a response telling the controller which activity/phase/step it should go to.

That way your work flow model is easy to test but it still doesn't know about your view technology.

于 2013-05-21T12:34:48.080 回答
0

Many mentioned in comments these thoughts, so here is a summary:

The logic to figure out whether you need a redirect and what your redirect should be belongs into the controller. The model simply takes the data a view needs. This happens AFTER you decided which view to render. Think of the redirect as an instruction to perform a different controller action.

I use ASP.NET MVC and the controllers generate a RedirectResult for this purpose, which are completely unit testable. I don't know what is supported in your framework, but this is what MVC would do:

public class MyController : Controller {

    public ActionResult ShowInfo(string id) {
         if( id == null ) {
             return new RedirectResult("searchpage");
         } else {
             return new ViewResult("displayInfo");
         }
    }
}

In your unit tests, you instantiate MyController and check the type of the result and optionally, the url or view name.

Whether your redirect is actually performed is not a unit test issue - that's essentially making sure your framework is working right. What you need to test is that you are giving the correct instruction (i.e. the redirect) and the correct url.

于 2013-05-21T14:35:39.387 回答