0

Routes.php

use MyMVC\Core\Route;

$route = new Route;
$route->add('/', 'HomeController@index');
$route->add('about', 'AboutController@index');
$route->add('contact', 'ContactController@index');

Index.php

<?php
/**
 * Define Constants
 */
define('BASE_PATH', dirname(realpath(__FILE__)));
define('APP_PATH', BASE_PATH . "/app");

/**
 * Including the Composer's autoloader
 */
require_once 'vendor/autoload.php';

/**
 * Load the routes declarations
 */
require_once 'app/routes.php';

/**
 * Bootstrap our application
 */
require_once 'app/init.php';

/**
 * Initialize our beautiful framework
 */
$application = new \MyMVC\Application($route);

composer.json

"autoload" : {
    "psr-4" : {
        "MyMVC\\" : "app/"
    },
    "classmap": [
        "app/Controllers",
        "app/Helpers"
    ],
    "files": ['app/routes.php']  // already removed this line
},

When using require_once it is giving undefined variable route error while if i use only require it shows the route object.

Why is that so ?

Raheel
  • 8,716
  • 9
  • 60
  • 102

2 Answers2

1

All symptoms suggest that 'app/routes.php' is included somewhere else before. Since it defines a variable, if such include does not happen in global scope the variable will be local to wherever it's called from.

Apart from using a dedicated debugger like Xdebug, you can use builtin tools to diagnose the issue. For instance, you have get_included_files() to get a list of included files in a given point. You can also add debug_print_backtrace() on top of 'app/routes.php' to find out where it's called from.

Note on updated question and follow-up comment: if you're trying to auto-load the file and the file gets loaded automatically, I'd say you've just answered your own question. But it's worth noting that auto-loading is intended to be used on functions and class definitions. You have an arbitrary code snippet that defines a variable and —as you've just learnt— since the variable becomes local to the auto-loader method it isn't of much use.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • I had much help with your answer but there is one thing i am trying to find out for many days. I used files autoloading via composer and i loaded routes.php. Doesn't this mean that the routes.php will b available automatically now ? – Raheel Sep 29 '15 at 11:13
1

This kind of error only happens when the file is already loaded from other files. That's why it's not loading it again and you are not getting instance of $route object.

E.g,

Let's say file1.php included routes.php

Now if you use:

  1. require 'routes.php' (it will load same file again even already loaded)

  2. requier_once 'routes.php' (it will not load the file if already loaded, and as you are not getting instance of $route variable, it's mean it's happening)

Nouman Arshad
  • 593
  • 2
  • 7
  • Point 2 says (it will not load the file if already loaded). If that is so, still $route instance should be available. – Raheel Sep 29 '15 at 10:51
  • but if and only if the file is not loaded in a function or class as variable instance then can only get from there – Nouman Arshad Sep 29 '15 at 11:16
  • yes you can see in my code above the routes.php is not a class nor a function. – Raheel Sep 29 '15 at 11:17
  • 1
    Your simple answer is that use GLOBAL $route; and now you will always get that variable even if loaded in any scope. Hope you get it :) – Nouman Arshad Sep 29 '15 at 11:28