Refactoring require_once file in a project
Asked Answered
U

1

0

I'm a beginner in PHP. And i'm working on project with this directories hierarchy : model, control, view and helper folders are in my project folder

Now i'm trying to write a file init.php and require_once it in each of control and model files, here's my init.php

<?php

    $current_dir = basename(getcwd());
    $model_dir = "model";
    $helper_dir = "helper";

    function require_helper(){
        $handle = opendir("../{$helper_dir}");
        while($file = readdir($handle)){
        if($file != "." && $file != ".."){
                require_once "../{$helper_dir}/{$file}";
            }
        }
    }

    if($current_dir == "control"){
        $handle = opendir("../{$model_dir}");
        while($file = readdir($handle)){
            if($file != "." && $file != ".."){
                require_once "../{$model_dir}/{$file}";
            }
        }

        require_helper();

    } elseif( $current_dir == "model") {
        $handle = opendir($current_dir);
        while($file = readdir($handle)){
            if($file != "." && $file != ".."){
                require_once "{$file}";
            }
        }

        require_helper();
    } 
?>

But when i test my project i get this error :

Notice: Undefined variable: session in C:\wamp\www\harmony\control\login.php on line 11

Here's my login.php file :

<?php
    require_once "../helper/init.php";
?>

<?php

    if(isset($_GET["logout"]) && $_GET["logout"] == "true" ){
        $session->logout();
    }

    if($session->is_logged_in()){
        redirect_to("../view/index.php"); 
    }

    if(isset($_POST["submit"])){
        $username = $db->escape_value($_POST["username"]);
        $password = $db->escape_value($_POST["password"]);
        $password = hash('sha1' , $password);
        $arr = User::auth($username , $password);
        if($arr){
            $usr = $db->instantiate($arr);            
            $session->login($usr);
        } else {
            Session::notify("Invalid login information.");
        }
    }    

?>

So could you help me please ? what's wrong is going on ?

Uric answered 30/7, 2011 at 17:33 Comment(5)
try adding if(isset($session) && $session->is_logged_in()){ to that condition. That should get rid of the error.Symphonic
Reuiring init.php in all files? You're doing it wrong! Look up front controller design. Basically you should have this one file that handles all requests and loads necessary files. This reduces amount of repeated boilerplate code dramatically.Portauprince
@Portauprince Can you provide me with a good php tutorial for front controller design ? thanks :)Uric
Not really. I've yet to write one ;P THe idea however is to have your index.php decide what files/classes to require and execute basing on URL parameters (which go into $_GET array. All PHP MVC frameworks I looked into use a variation of this scheme.Portauprince
Oh, so i can modify init.php so i can make use of $_GET variable ? or just have to start over.Uric
E
1

You're trying to access $current_dir, $model_dir and $helper_dir inside of functions. You can't access variables which were declared outside of a function unless they are declared global, or else actually passed into the function.

so for example:

function require_helper(){
    global $helper_dir;//this is key
    $handle = opendir("../{$helper_dir}");
    while($file = readdir($handle)){
    if($file != "." && $file != ".."){
            require_once "../{$helper_dir}/{$file}";
        }
    }
}
Euton answered 30/7, 2011 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.