Jump to content

Profile Page on MVC pattern


hariharen

Recommended Posts

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]

Link to comment
Share on other sites

<?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;
   }

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.