PHP Frameworking - Routing & Autoloading & Configuration (Part 2)



zooboole Since Jan 22, 2015 117 Posts

If you have a php version lower than PHP 5.4 use this:|

private $autoloadable = array();
Auto Default Since Apr 21, 2015 4 Posts

First off, thanks for making this tut. I got an issue though. I am getting a fatal error having to do with the BASEPATH constant:

Notice: Use of undefined constant BASEPATH - assumed 'BASEPATH' in C:\xampp\htdocs\mvc-PHP2\bootstrap.php on line 8

Fatal error: Uncaught exception 'Exception' with message 'View does not exist: BASEPATH/views/hello.php'

Followed everything exactly, just can't get past. Maybe I'm missing something simple, please enlighten me.

Auto Default Since Apr 21, 2015 4 Posts

I just could not let it go. I kept at it. Something so simple as a constant that just doesn't seem to work was racking my mind, I could understand if it was some very complex operations but... Anyhow I stepped back, relaxed and cleared my head by just watching a little tv and throwing in a movie which I skipped through. In any event, that kinda allowed me to look at what was going on from a high level, i.e What is this constant BASEPATH doing anyway ? What's it purpose to begin with ? Its just a pointer to the root directory at the end of the day. Why does it need to be a in its own separate file "config.php" when "bootstrap.php" is at the same directory level ? It doesn't. So I did away with requiring "config.php" altogether. No need. Just moved defining BASEPATH to "boostrap.php"

/*boostrap.php */
define('BASEPATH', dirname(realpath(__FILE__))); 
// Samething
define('BASEPATH', __DIR__);

I don't know why this caused an error in the first place.

zooboole Since Jan 22, 2015 117 Posts

Auto default, The config.php becomes very useful when your project becomes bigger. This file could contain other settings your app could need. Now for you bringing it into bootstrap does not cause any issue as far as I am concerned.

But one thing you should also consider is your PHP Version. Can you please tell us the version of PHP you are using? Thanks.

zooboole Since Jan 22, 2015 117 Posts
Auto Default Since Apr 21, 2015 4 Posts

Zooboole, I'm running PHP 5.6.3 and I did play around with the directory path as per your suggestion but some reason BASEPATH is just not making it to /public/index.php

And I agree with about "config.php" and it role down the road but for now I'm going to go with having BASEPATH in "bootstrap.php" so I can move forward with this tutorial. Afterwards I'll come back to it.

zooboole Since Jan 22, 2015 117 Posts

Great, I will also give a closer look at it. If anything I will surely let you know.

Auto Default Since Apr 21, 2015 4 Posts

I added the router class, instantiated it in "bootstrap.php" and added the routes in separate "routes.php" file which is required in "index.php" but I'm still getting a 404 not found! page

404 - /mvc-PHP2/public/about-us was not found!;

after this code is in place:

$router->add('/about-us', function() use ($view) {
$view->display('about.php');

And I did create a test page "about.php" in the views folder.

zooboole Since Jan 22, 2015 117 Posts

Auto Default you need to make the autoloading very well. @Matej made it like this:

$autoloader = new Autoload();

spl_autoload_register([$autoloader, 'load']);

$autoloader->register('viewloader', function(){
    return require(BASEPATH.'/core/view/viewLoader.php');
});

But you need to do this before it work:

$autoloader = new Autoload();

$autoloader->register('view', function(){
     return require(BASEPATH . '/core/view/view.php');
});

$autoloader->register('viewloader', function(){
     return require(BASEPATH . '/core/view/viewLoader.php');
});

spl_autoload_register([$autoloader, 'load']);

Can you see the difference?

Aside that in core/autoload/autoload.php @Matej did the load() method like this:

public function load($name){
    $name = strtolower($name);
    $filepath = BASEPATH.'/core/'.$name.'/'.$name.'.php';
    if( !empty($this->autoloadable[$name]) ){
        return $this->autoloadable[$name]($name);
    }
    if( file_exists($filepath) ){
        return require($filepath);
    }
    throw new Exception($name.' is not loaded or registered for autoloading');
}

But you have to make like this before it works:

public function load($name)
{
    $name = strtolower($name);
    $filepath = BASE_PATH . "/core/".$name."/".$name.".php";


    if ( !empty($this->autoloadable[$name]) ) {
        return $this->autoloadable[$name]($name);
        exit;
    }

    if (file_exists($filepath)) {
        return require $filepath;
        exit;
    }

Aside that I think your other issues should be coming from your BASEBATH

Since Jul 24, 2016 1 Posts

@Auto Default: i've same problem of you, because following line code in dispatch() function of "Router.php" file is uncorrect: <pre><code> if( $url == $SERVER['REQUESTURI'] ) </code></pre> Add this line of code just before the foreach cycle: <pre><code> $queryaction = isset($SERVER['PATHINFO']) ? $SERVER['PATHINFO'] : "/"; </code></pre> and rewrite previous comparing line of code as follow: <pre><code> if( $url == $queryaction ) </code></pre> Now View files "home.php" and "about.php" should be called correctly.



Since Jul 27, 2016 1 Posts

Question regarding the setNotFound method. How would I use that if I wanted to redirect to a views/errors/404.php file?

I tried calling the setNotFound method in several places but guess I'm just not understanding it.

Any suggestions?

Thanks!

Artur Cesar Jacinto Since Oct 03, 2016 3 Posts

Routing trouble.

I loved this tutorial. Before i share my doubts, a sugest: Use autoload() instead splautoloadregister(). This last is vary verbous while "autoload()" do everithing what you need with namespaces.

My doubt/trouble is about routing system. First, i had to modify the function dispatch() adding another condition on "if"

foreach ($this->routes as $url => $action) {

        if($url == $_SERVER['REQUEST_URI'] || $url == '/'){
            return $action();   
        }
    }

becouse the $SERVER['REQUESTURI'] returns to me a string "/framework/public/" and in the route declaration, just "/". If you consider this a error or a bad use pratice, please, tell me how can i fix this. At last but not least, when a use another route, like '/about-us', the server returns to me a erro 404; I guess that "framework/public/" searches for the public directory. If i try to do "framework/public/about-us" , the server will look for the about-us directory before the routin system can get that and use to retrive-me a view declared on:

$route->('/about-us', function() use ($view){$view->('about.php');});

i'm using php v 5.6+, windows and yes, i've been created the views/about.php

So, thanks your share your knowledge and habilities. You are vary smart. If you could help-me i'll be vary thankfull!

Since Oct 05, 2016 0 Post

nice 1 pal

zooboole Since Jan 22, 2015 117 Posts

Artur Cesar Jacinto, let me give it a closer look. Meanwhile can you try PHP's in-built server with the -t option like:

php -S 127.0.0.1:8888 -t public/

This should be returning a better string.

Felix Mensah Since Oct 19, 2016 1 Posts

hi