hariharen Posted November 10, 2011 Share Posted November 10, 2011 Hi, I am using jream's MVC pattern and building a new movie review site, But i have some problem on profile page, i am trying to show the profile page when 'http://localhost/Myname', I use .htaccess file too.. I fetch that 'Myname' from url & i can find the controller file, but the problem is I don't kno how to check weather the username exists in database, If it exists I need to diaplay the profile page f that person else to the error page.. I tried many time, but m not getting how to do it.. Here's my code.. <?php class Bootstrap { function __construct() { $url = isset($_GET['url']) ? $_GET['url'] : null; $url = rtrim($url, '/'); $url = explode('/', $url); if (empty($url[0])) { require 'controllers/index.php'; $controller = new Index(); $controller->index(); return false; } // M struck with this part $file = 'controllers/' . $url[0] . '.php'; if (file_exists($file)) { require $file; } else { $this->error(); } $controller = new $url[0]; $controller->loadModel($url[0]); // calling methods if (isset($url[2])) { if (method_exists($controller, $url[1])) { $controller->{$url[1]}($url[2]); } else { $this->error(); } } else { if (isset($url[1])) { if (method_exists($controller, $url[1])) { $controller->{$url[1]}(); } else { $this->error(); } } else { $controller->index(); } } } function error() { require 'controllers/error.php'; $controller = new Error(); $controller->index(); return false; } } anyone please help, M struck with this [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/ Share on other sites More sharing options...
trq Posted November 10, 2011 Share Posted November 10, 2011 the problem is I don't kno how to check weather the username exists in database Then post the relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/#findComment-1286908 Share on other sites More sharing options...
hariharen Posted November 10, 2011 Author Share Posted November 10, 2011 <?php class Bootstrap { function __construct() { // Here I fetch the http://localhost/"Myname" "Myname" ie, http://localhost/index.php?url=Myname $url = isset($_GET['url']) ? $_GET['url'] : null; $url = rtrim($url, '/'); $url = explode('/', $url); if (empty($url[0])) { require 'controllers/index.php'; $controller = new Index(); $controller->index(); return false; } // M struck with this part // Here in this part m finding the file like "index page, login page, registration page etc.." // Here I want to check the database to find the username & render profile page if username exists. $file = 'controllers/' . $url[0] . '.php'; if (file_exists($file)) { require $file; } else { $this->error(); } $controller = new $url[0]; $controller->loadModel($url[0]); // calling methods if (isset($url[2])) { if (method_exists($controller, $url[1])) { $controller->{$url[1]}($url[2]); } else { $this->error(); } } else { if (isset($url[1])) { if (method_exists($controller, $url[1])) { $controller->{$url[1]}(); } else { $this->error(); } } else { $controller->index(); } } } function error() { require 'controllers/error.php'; $controller = new Error(); $controller->index(); return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/#findComment-1286912 Share on other sites More sharing options...
hariharen Posted November 12, 2011 Author Share Posted November 12, 2011 Anybody please Help :'( Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/#findComment-1287516 Share on other sites More sharing options...
gizmola Posted November 12, 2011 Share Posted November 12, 2011 You should be making a controller file and putting the code for the method in it. Your url should be something like: localhost/profile/username. Your controller would then be named profile.php. You should not be editting the bootstrap file and putting in controller specific code. Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/#findComment-1287517 Share on other sites More sharing options...
hariharen Posted November 13, 2011 Author Share Posted November 13, 2011 You should be making a controller file and putting the code for the method in it. Your url should be something like: localhost/profile/username. Your controller would then be named profile.php. You should not be editting the bootstrap file and putting in controller specific code. But Actually I need an URL Like http://localhost/Myname Is that not possible with this code? Please Help.. the http://localhost/profile/Myname looks little bit ugly.. so.. Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/#findComment-1287742 Share on other sites More sharing options...
gizmola Posted November 13, 2011 Share Posted November 13, 2011 Anything is possible, but I would not advise that. Basically what you would need to do is have an htaccess rule that looked for controllers first, and matched those. Any names that are the same as controllers are going to be a problem. Regardless, they are still going to redirect to a controller while passing a param. Quote Link to comment https://forums.phpfreaks.com/topic/250819-profile-page-on-mvc-pattern/#findComment-1287766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.