I am trying to develop my first "big project" in php. I used the simple open-source MVC framework provided by traversy. You can download it here
I decided to use a lot of ajax & jquery because of the nature of the project. I am getting to a point where I need to reload small parts of the page whithout refreshing the whole page. That's why I use the .load()
jquery function. The part I want to load is in my app/views folder & needs to access php $_SESSION
variables (it's not a static view and I don't want to store it in the 'public' folder even though it would kind of work this way).
The problem is that the view is not loading because of how the framework is made (loading controllers based on urls). Here is the 'Core.php' file of the framework :
<?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(){
$url = $this->getUrl();
// Look in controllers for first value
if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 Index
unset($url[0]);
}
// Require the controller
require_once '../app/controllers/'. $this->currentController . '.php';
// Instantiate controller class
$this->currentController = new $this->currentController;
// Check for second part of url
if(isset($url[1])){
// Check to see if method exists in controller
$url[1] = dashesToCamelCase($url[1]);
if(method_exists($this->currentController, $url[1])){
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
// 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'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
Here is the 'Pages.php' controller :
<?php
class Pages extends Controller {
public function __construct(){
}
public function index(){
/*$data = [
'title' => 'Welcome'
];*/
if(isLoggedIn()){
redirect('projects');
} else {
redirect('users/login');
}
$this->view('pages/index', $data);
}
public function about(){
$data = [
'title' => 'About Us'
];
$this->view('pages/about', $data);
}
public function newProject(){
$data = [
'title' => 'About Us'
];
$this->view('cdp/new-project', $data);
}
}
?>
And here is the Controller.php file :
<?php
/*
* Base Controller
* Loads the models and views
*/
class Controller {
// Load model
public function model($model){
// Require model file
require_once '../app/models/' . $model . '.php';
// Instatiate model
return new $model();
}
// Load view
public function view($view, $data = []){
// Check for view file
if(file_exists('../app/views/' . $view . '.php')){
require_once '../app/views/' . $view . '.php';
} else {
// View does not exist
die('View does not exist');
}
}
}
The problem obviously happens in the index()
function in the Pages.php file since the loaded part displays the 'projects' page. But that's because it is the default behaviour if the controller file doesn't exist (Core.php).
How to change this behaviour and prevent the redirect ? Maybe I need to rewrite Core.php or Pages.php completely but I have no idea how since the framework part was a tutorial.
I hope you have enough infos, tried to be as clear as possible. Thanks.