2

I know I can do this:

//Jquery
$.ajax({type: 'POST', data : {'action' : 'foo'}});

//PHP
if(isset($_POST['action']) && $_POST['action'] == 'foo')
{
    function foo()
    {
        //yeah
    }
}

But.. In a project I'm working on with a friend, he has set up the controllers to be able to have specific functions called using custom actions. For instance, say my view is using the controller of thing.php. From my Javascript I can just AJAX to a url like this:

'url' : 'thing'

So in this case, foo() would get called without a need for any ifs or switches (as far as I know)

To me, this is great and ideal but I'm not sure how it was set up. He isn't around for the holidays to ask so I'm asking you guys.

Do you know how this is achieved? We are using a pretty typical MVC architecture. Sorry I am a PHP novice. Thanks!

8
  • Why don't you ask your friend what and how he implemented? Commented Dec 28, 2012 at 9:38
  • @EaterOfCorpses: excuse me? Commented Dec 28, 2012 at 9:39
  • 1
    @DCoder: so? "Someone has implemented something. How does it work?" --- could you answer this? Commented Dec 28, 2012 at 9:40
  • 1
    @zerkms: not with such a vague description, no. But I would explain why I can't answer it, not just tell them to do what they already said they can't do right now. Commented Dec 28, 2012 at 9:42
  • 2
    I (and afraid that nobody else on SO) have idea how your friend has done this. You can read his/her script. In general, classes are called either dynamically: $class = new $classType(); $class->$actionName(); or loaded with reflection (the last one is slightly slow but gives more information that maybe used in advanced pre-processing. Commented Dec 28, 2012 at 9:42

1 Answer 1

7

It looks like your friend is using .htaccess to rewrite URLS to add .php, perhaps with

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Now if you call a URL like /thing it will actually call the file /thing.php on your server and execute it. Or in your case if your url is just thing without the starting / it will call the thing.php in the same folder your current page is in.

Or perhaps he is rewriting everything to the controller and then adding the variable as command. Something like

RewriteRule ^(.*)$ controller.php?action=$1

Anyway, check your/his .htaccess file for clues

Sign up to request clarification or add additional context in comments.

2 Comments

by the way .htaccess files are often hidden files by default so you won't necessarily see them unless you have hidden files showing.
btw2> my hosting company uses iirf.ini file instead of .htaccess, so perhaps you could look at that also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.