0

Ive used a common approach to a custom MVC pattern

controller/action/param

$controller->action($param)

How can I implement a method of redirecting i.e. using header() because theres always an initial output from the main controller but Im not intentionally outputting anything.

NOT INTERESTED IN USING POPULAR FRAMEWORKS AND IM NOT TRYING TO REINVENT THE WHEEL JUST TRYING TO LEARN.

Steve Took
  • 161
  • 3
  • 10

3 Answers3

4

you can enable output buffering to store the initial output and flush it after the header function

ob_start();
echo "output";
header("Location: other.php");
ob_end_flush();

or you can use javascript to redirect:

echo '<script type="text/javascript">window.location='.$url.'</script>';

EDIT:

There is a common case when writing class, you accidentally leave a whitespace after the closing ?> tag, maybe it's causing the output.

to avoid such accidents just leave out the closing ?>, it's completely legal, unless you have only php in your file.

Headshota
  • 21,021
  • 11
  • 61
  • 82
  • Thanks Headshota, both good answers but they just appear so messy. I was hoping to understand more about MVC and what the main controller is outputting because I cant see anything in particular being output. – Steve Took Sep 19 '11 at 09:23
  • I can't guess what's your controller outputting, the error message during redirection should prompt you where the output started. – Headshota Sep 19 '11 at 09:25
0

One way is to buffer all the output with ob_start functions. And in your redirect function you can use the header.

Another approach would be to send a meta tag for html redirect.

Dan Bizdadea
  • 1,292
  • 8
  • 15
0

I wouldn't redirect, it is not necessary and may be time consuming (more requests, responses to/from the server)

I would use index.php (as a main controller) and handle any request to the server, then calling to the requested controller action and then calling the view to render the requested view, and finally the main controller sending the rendered view to the client.

Doing it this way you can use the index.php as a main controller or main app, and centralize anything you need to (segurity, cache, debug, logs).

In order to get the index controller handle every request you can use mod_rewrite.

This is one approach I did for my MVC, maybe this helps to you on understanding the flow (this is not the only one approach, check wikipedia or resources).

MAIN CONTROLLER: catches the request, and invokes the appropiate controller with the request data (POST / GET)

CONTROLLER: executes the action involved, calling models to retrieve the data, and sending the output data to the view, getting the output from the view and sending it back to the main controller.

MODEL: gets data from the database and operates it

VIEW: uses layouts, templates, and incoming data to build the html output

Packet Tracer
  • 3,884
  • 4
  • 26
  • 36
  • I wouldn't output anything from the main controller, just echo the rendered output by the view as "last" action. Things to be shown must be generated/handled by the view. – Packet Tracer Sep 19 '11 at 09:23
  • Thanks for your efforts Feida but this is much the same approach as I have already used, just appears to be some output from the main controller that is not intended causing the issue with redirects. – Steve Took Sep 19 '11 at 09:37
  • main controller should not output, you should fix that – Packet Tracer Sep 19 '11 at 12:29