Discussion – Lesson 4 - Wiring controllers and views

Back

 

Comments
Avatar
Agostino I
Member
Avatar
Replies to David Capka Hartinger
Agostino I:7/10/2019 11:56

We redirect to ErrorController, but then is however exectued the following code of the routerController:

// Calls the controller
$this->controller->process($par­sedUrl);

// Setting template variables
$this->data['title'] = $this->controller->head['title'];
$this->data['descrip­tion'] = $this->controller->head['descrip­tion'];
// Sets the main template
$this->view = 'layout';

right?

Reply
7/10/2019 11:56
while(1){ workHard();} but sometimes....take a rest! :)
Avatar
Replies to Agostino I
David Capka Hartinger:7/11/2019 4:54

The line $this->redirect('error'); tells the browser to change the URL and stops the script immediately:

public function redirect($url)
{
    header("Location: /$url");
    header("Connection: close");
        exit;
}

When a new URL is opened (error in our case), the code of the RouterController is executed as you've mentioned.

Reply
7/11/2019 4:54
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Agostino I
Member
Avatar
Replies to David Capka Hartinger
Agostino I:7/13/2019 9:33

But if the script is stopped, so the code that is after

else
                        $this->redirect('error');

that is:

// Calls the controller
                $this->controller->process($parsedUrl);

                // Setting template variables
                $this->data['title'] = $this->controller->head['title'];
                $this->data['description'] = $this->controller->head['description'];
                $this->data['messages'] = $this->getMessages();
                // Sets the main template
                $this->view = 'layout';

should not be executed...or we have the redirect and is however processed the code at the end of RouterController?

I am in trouble to understand if,after the redirection, is however executed the remaining code or if it's stop the execution: I see that is however processed because it is set the view at 'layout' value, but so redirect doesn't stop the script...

Reply
7/13/2019 9:33
while(1){ workHard();} but sometimes....take a rest! :)
Avatar
Agostino I
Member
Avatar
Replies to David Capka Hartinger
Agostino I:7/23/2019 12:50

I think I got the point:

if there isn't a valid URL, error becomes the URL and $router->process(arra­y($_SERVER['RE­QUEST_URI']));

give to process method of routerController the string "error" and so the ball is passed to the errorController, right?

Reply
7/23/2019 12:50
while(1){ workHard();} but sometimes....take a rest! :)
Avatar
Reply
7/24/2019 5:16
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Bill Blalock
Member
Avatar
Bill Blalock:11/25/2019 19:29

I get a message indicating the substantiation failed on this line of RouterController
<code>$this->controller = new $controllerClas­s;</code>

The value of $controllerClass is "Controller".

The test
<code>if (file_exists('con­trollers/' . $controllerClass . '.php'))</code>
returns true.

So
<code>$this->controller = new $controllerClas­s;</code>
translates to
<code>$this->controller = new Controller;</code>

That is the cause of the error. My research indicates that an Abstract class, like Controller, cannot be instantiated.

What am I missing or what have I done wrong?

 
Reply
11/25/2019 19:29
Avatar
David Capka Hartinger:11/27/2019 12:52

Hi Bill, have you tried running the attached code? Perhaps you made a mistake somewhere. I've downloaded it and opened it, all worked fine for me.

Edited 11/27/2019 12:54
Reply
11/27/2019 12:52
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Rudolf Völgyi:12/17/2019 11:18

Hey,

I started learning mvc, and this is the best aritcle I've found about this topic.
I followed your instructions step by step but I always get the browser's default error message: "The requested URL /error was not found on this server.".
I've figured out the problem is that I put the whole project into a folder called mvc.
I spent hours to find the solution, but slowly I'm going to tear out my hair.

I would be thanful for any ideas!
Rudi

 
Reply
12/17/2019 11:18
Avatar
roman p
Member
Avatar
Replies to Rudolf Völgyi
roman p:6/12/2020 16:28

did you just move it to the root folder or you've changed the path somewhere?

i'm tryin to change the path but can't figure it out. php noob :/

great article so far! really thorough. coupled it with stepping through the xdebug to really let the concepts sink in.

 
Reply
6/12/2020 16:28
Avatar
Replies to Rudolf Völgyi
Shane Muirhead:5/13/2021 8:12

I had the same issue as Rudolf, I'd put my entire project in a sub folder in root called 'MVC' as I have several projects on my XAMP. This caused the whole app not to work and any url entered resulted in an generic error but not the error from my MVC app.

Moving the full project to root fixed the issue and now I can progress but I'm keen to learn how to keep it within a subfolder and still have my app work if anyone can explain that for me

 
Reply
5/13/2021 8:12
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

10 messages from 23 displayed.