0

I've been struggling with a project and I can not seem to work it out correctly. My goal is to create a PHP mvc framework that uses Ajax calls to show it's contents. So what do I mean exactly?

  • When you go to {urL}/person/getall, the framework should call function 'getall' from controller 'person'.
  • When you click on a link it should get the contents from that link through ajax so it does not reload the page.

So here comes the issue, I've created a basic framework that works. When you go to {urL}/person/getall, the framework calls the function 'getall' from controller 'person'. And all links are handled by Ajax calls so the contents get showed, lovely. But the problem is, the index.php initializes all classes. As so as I call a link through Ajax, in the background it initializes index.php again. So every class gets re-instantiated. And this is a problem for me because I need to store variables in classes to re-use them.

Let me explain some of my 'architecture'.

.htaccess

  • RewriteBase /jsonmvc/ Options +FollowSymLinks RewriteEngine on

  • RewriteRule ^([a-zA-Z])/?([a-zA-Z])?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&input=$3 [NC,L]

index.php The index.php contains initializations for models and other classes. Most important Loader.php

Loader.php This one includes the controllers classes and instantiates them. Also ensures that the routing is done (pointing to the right controller and action).

And then we ofcourse got the MODELS/VIEWS/CONTROLLERS.

So the issue is, with an ajax call all the classes get re-instantiated because index.php gets reloaded and so do all the other files.

How can I avoid this? I need the ajax calls to just call the specific controller/action. Is this even possible by using the routing? Or is it only possible by doing this? : using jquery $.ajax to call a PHP function

I'd really like it to be possible with the /player/getsomething.

Community
  • 1
  • 1
Serellyn
  • 405
  • 1
  • 9
  • 26

0 Answers0