0

I am new to Mvc, what I needed to do was try to get the method "about" from "pages" but it shows the error 404, url not found, but the "pages" does work for the "__construct".

When I type the link-> "http://localhost/MVC/Public/pages/about", it should show the detail in the method "about" which is present in the file "pages.php".

"http://localhost/MVC/Public" this url works well and "pages.php" which is the default is used and the data in the "__construct" is shown.

THIS IS PAGES.PHP

<?php
    class Pages{
        public function __construct(){
            //echo 'Pages Loaded';
        }

        public function index(){

        }
        
        public function about(){
            echo "this is about";
        }
    }

THIS IS CORE.PHP

<?php
    /* 
        * App Core Class
        * Creates URL & loads core controller
        * URL FORMAT -/controller/method/params
    */

    class Core{
        protected $currentController = 'Pages'; 
        protected $currentMethod = 'index';
        protected $params = [];

        public function __construct(){
            //print_r($this->getUrl());
            $url = $this->getUrl();

            // Look in controllers for first value...
            if($url && file_exists('../app/Controllers/' . ucwords($url[0]) . '.php')) { // checks if the url exists and
                // also if the .php file exists...
                // it exists, set as controller..
                $this-> currentController = ucwords($url[0]);
                // Unset 0 index...
                unset($url[0]); 
            }
            // requires the controllers...
            require_once '../app/controllers/' . $this-> currentController . '.php';

            // Instantiate controller class...
            $this-> currentController = new $this-> currentController;
            
            // check for the second part of the url i.e. url[1]...
            if(isset($url[1])){
                // check to see if the method exists in the controller...
                if (method_exists($this->currentController, $url[1])){ // method_exists ( $object, $method_name );
                    $this->currentMethod = $url[1];
                    // unset 1 index..
                    unset($url[1]);
                }
            }
        
            //echo $this->currentMethod;
            

            // get params
            $this->params = $url ? array_values($url): [];

            // call a callback with array of params
            call_user_func_array([$this->currentController, $this->currentMethod], $this->params);

        }

        public function getUrl(){
            if(isset($_GET['url'])){
                $url = rtrim($_GET['url'], '/'); // works simialr as split() in py...
                $url = filter_var($url, FILTER_SANITIZE_URL); // it won't have any charactors that a url should not have...
                $url = explode('/', $url); // to simply turn the url into an array after every '/'...
                return $url;
            }
        }
    }

ANY SUGGESTIONS WHICH COULD WORK FOR THIS

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • I'm finding references to this code on [SO](https://stackoverflow.com/q/64510589/231316) and [GH](https://github.com/codewithdary/login-register-script-mvc/blob/master/mvcloginregister/app/libraries/Core.php). Are you following a tutorial? If so, it might be best to ask the person that wrote this. If that's not an option, it might be better to use a library or router or something that has community feedback options. There's nothing wrong with this code (that I see at a quick glance) but there might be better, more supported things. You also might just be fighting Apache and not PHP. – Chris Haas Jan 24 '23 at 19:40

0 Answers0