Jump to content
Lizard Wizard Coding Club

Kp5-Dispatching-Views Errors


Recommended Posts

Hello.

 

I have been going through your php training video and i have done all up to php_advanced_pt2 which includes :

 

kp3-setting-up-htaccess-for-controllers.mov

kp4-dispatching-requests-to-multiple-controllers.mov

kp5-dispatching-views.mov

 

 

The first video seems fine but somewhere in kp4 and kp5 it all goes wrong and it doesn't work how it should.

 

I have been over and over it and can't find the problem. I have even installed your own files found in "php_advanced_sourcefiles / kp5-dispatching-views " and i am getting exactly the same errors.

 

I am using mamp pro and i have tried the script on php 5.2.17, php5.3.14 and php 5.4.4 which i am currently using.

 

 

My files are located in:

 

/Users/rickyspires/Sites/TRAINING/PHP_OOP/MVC/public

 

 

these are the errors i am getting:

 

 

 

Notice: Undefined offset: 2 in /Users/rickyspires/Sites/TRAINING/PHP_OOP/MVC/public/index.php on line 22

 

Fatal error: Uncaught exception 'Exception' with message 'Please implement a function called Action!' in /Users/rickyspires/Sites/TRAINING/PHP_OOP/MVC/site/app/controllers/ControllerAbstract.php:31 Stack trace: #0 /Users/rickyspires/Sites/TRAINING/PHP_OOP/MVC/public/index.php(37): site\app\controllers\ControllerAbstract->dispatch() #1 {main} thrown in /Users/rickyspires/Sites/TRAINING/PHP_OOP/MVC/site/app/controllers/ControllerAbstract.php on line 31

 

 

 

If i remove $pfx from list($pfx, $controllerName,$actionName) = preg_split('/[\/\\\]/',$uri); in index.php

it fixes the first problem because $pfx is not defined any where but it is probably not the correct thing to do.

 

 

and

 

 

if i remove

 

else

throw new \Exception("Please implement a function called $method!");

 

from ControllerAbstract.php it fixes the second error but is probably not the correct thing to do.

 

 

 

when i remove that code and go to my local site (http://training-mvc.com'>http://training-mvc.com) the site is blank

 

if i got to:

 

http://training-mvc.com its blank

http://training-mvc.com/index/ its blank

http://training-mvc.com/index/welcome its blank

 

 

 

 

Thank you

Ricky Spires

Edited by rickyspires
Link to post
Share on other sites

I seem to have a similiar problem I get the error messages

Notice: Undefined variable: controller in c:\xampp\htdocs\playground\killer_php\public\index.php on line 45

 

Fatal error: Call to a member function dispatch() on a non-object in c:\xampp\htdocs\playground\killer_php\public\index.php on line 45

 

Line 45 contains

 

$controller->dispatch();

Just to be on the save side I exchanged my code with the download code so I'm sure there is no typo - has anyone got an idea?

 

Regards

mantis

Link to post
Share on other sites

After the above problem did not give me any peace I played around with the index.php file and came to the conclusion that the problem lies with the URI

 

I use xampp 1.8.3 (php 5.5.1, Apache 2.4.4)

 

My File path is:

F:\xampp\htdocs\killer_php (htdocs is the root for localhost)

 

If I echo print some of the variables I get:

echo APPLICATION_PATH .'<br />';

I get F:\xampp\htdocs\killer_php

 

so far so good

The next thing I echoed was the REQUEST_URI on index in the public folder

echo '<br />Request URI: ' . $uri . '<br />';

This outputs

Request URI: /killer_php/public/index/

 

Also this is still good

However is I echo the controller Name after:

list($pfx, $controllerName,$actionName) = preg_split('/[\/\\\]/',$uri);

but before the switch I get

Controller Name: killer_php

 

Obviously this is wrong as it is supposed to be index, I tried to adjust the path but to no avail

 

I would be glad if someone could help or point me in the right direction

 

Regards

mantis

Link to post
Share on other sites

For all those that have the same problem as me here is a dirty workaround

 

In the file /public/index.php find the line

 

list($pfx, $controllerName,$actionName) = preg_split('/[\/\\\]/',$uri);

 

and replace with:

 

//list($pfx, $controllerName,$actionName) = preg_split('/[\/\\\]/',$uri);
$uri_e = preg_split('/[\/\\\]/', $uri);
$nrs = count($uri_e);


$nrs1 = ($nrs - 2);
/*
*    Uncomment the next 4 lines if you are not getting the right result
*    then adjust the number in the line above
*/
//echo '<pre>';
//echo 'The number of the array identifier :' . $nrs1 . '<br />';
//echo 'The value of the array node: ' . $uri_e[$nrs1] . '<br />';
//print_r(preg_split('/[\/\\\]/', $uri));

/*
*  This is where the "magic" happens
*/


if ($uri_e[$nrs1] === 'index' && $uri_e[$nrs1 + 1] === '' || $uri_e[$nrs1] === 'public' && $uri_e[$nrs1 + 1] === '') {
     $controllerName = 'index';
     $actionName = '';
} elseif ($uri_e[$nrs1] != '' && $uri_e[$nrs1 + 1] != '') {
     $controllerName = $uri_e[$nrs1];
     $actionName = $uri_e[$nrs1 + 1];
} elseif($uri_e[$nrs1] === 'test' && $uri_e[$nrs1 + 1] === '' ){
     $controllerName = 'test';
     $actionName = '';
}else {
     echo 'Controller can not be found';
}

/*
*    Which controller is passed ?
*    Uncomment the next line to find out
*/
//echo '<br />Controller Name: ' . $controllerName . '<br />';

 

Thats all, this way you can at least follow the rest of the tuts

 

Hope it helps

 

Regards

mantis

Link to post
Share on other sites
  • 2 weeks later...

As the above does not work that well and is all but elegant I sat down and refactored the code.

 

Firstly find the lines (of the original code)

$uri = strtolower($_SERVER['REQUEST_URI']);
list($pfx, $controllerName,$actionName) = preg_split('/[\/\\\]/',$uri);

and replace with:

$uri = strtolower($_SERVER['REQUEST_URI']);
$url = preg_split('/[\/\\\]/', $uri);

//See if you are on target by un commenting the following two lines index should be $url[3]
//echo '<pre>';
//print_r($url);


//list($pfx, $controllerName,$actionName) = preg_split('/[\/\\\]/',$uri); // this does not work unless you are in the root


if ($url[3] === "") {
     $controllerName = "index";
     $actionName = "";
} else {
     $controllerName = $url[3];
     $actionName = $url[4];
}

 

Regards

mantis

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...