Jump to content

Trying to find a simple PHP ReST Tutorial


DeX

Recommended Posts

I'm quite good at PHP but I thought it was time to learn this ReST thing people are talking about. As of right now I've got a web application written entirely in PHP and has a MySQL database full of......let's say users. Anyone logged in can browse a list of the other users (users.php) and can then visit that user's profile (user.php). There are no security limitations for the sake of our example. Right now when the user clicks another user, it brings them to a URL (http://www.example.com/user.php?user=123) and the page (user.php) makes a call to the model which checks the database for a user with ID 123, then returns any information for that user.

 

I'm wondering if it would be more professional to have the user go to a more friendly URL (http://www.example.com/users/bob) which would load user.php with all the information about Bob (only a single Bob in the database). Is it reasonable to switch from my way to this ReST way? Can someone point me (or provide) a tutorial for the simplest example you can imagine? I would just need it to get to the SQL call, I can do all the database authentication and calling myself, I just want to know how everything is set up and I can build on it from there. I can't find anything this simple online, they're all too complicated with extra features I don't want. Thanks.

Link to comment
Share on other sites

REST, at it's core, is really just about using the right HTTP verb for the job.  GET, POST, PUT, DELETE all have meanings, and if something is described as being RESTful, all it really means is that the site/app in question adheres to those meanings.

 

Clean URLs are nice, but they aren't REST.  The fact that you're using GET to retrieve that info is what makes it RESTful.  That said, friendly URLs are, well, friendly, and make it easier to facilitate REST.  In your example, there is nothing wrong with something like: example.com/users/123.  The URL conveys that you're retrieving (GET-ting) user 123.  There's no need to specify that it's Bob.  A real life example is Stack Overflow.  Their URLs are shaped like: stackoverflow.com/questions/<question id>/<slug>, but, only the question id is used in retrieving the question.  If you leave the slug off the end, it still gets the right one, and if you try retrieving a question with the slug alone, you'll get a 404.

Link to comment
Share on other sites

Very interesting, thank you. One last question, is it acceptable web programming for me to be using example.com/user.php?id=123 instead of example.com/users/123

I'd put a question mark at the end of that question but it looks confusing so I left it off. I'm hoping my site in question is going to blow up big, something like Kijiji and be used quite a lot on a daily basis. I want it to be as perfect as possible.

Link to comment
Share on other sites

  • 2 months later...

Okay, I've had success with mod_rewrite and .htaccess, I can now redirect  URL of http://www.example.com/users/bob to point to http://www.example.com/some-page.php?action=getUser&user=bob.

 

So now that I've been successful with that I'm a little confused how I would use this. If I wanted to display a page all about Bob and his associated information in the database, what do I do with this? Am I right to assume I would just have a regular user.php page which I would rewrite to and that would load all the information using $_GET['user'] to figure out which user to display? If so then that's simple enough but what about the API I keep reading about? I keep reading that I should create a rest API file so what would this look like and what is it used for?

 

As of now I have this, is this pointing in the right direction?

<?php require_once('include/include.php'); ?>

<?php
    $action = $_GET['action'];
    $userId = $_GET['user'];
    
    switch($action)
    {
        case("get"):
            $controller::getUserById($userId);
            break;
        case("delete"):
            $controller::deleteUser($userId);
            break;
    }
?>

My include.php file has the following:

<?php

    require_once('controller/mainController.php');
    require_once('model/mainModel.php');
    require_once('view/mainView.php');
    require_once('controller/loginController.php');

    // call on every page to start the session
    MainController::sec_session_start();

    $model = new MainModel();
    //It is important that the controller and the view share the model
    $controller = new MainController($model);
    $loginController = new LoginController($model);
    $view = new MainView($controller, $model);

//    if (isset($_GET['action']))
//        $controller->{$_GET['action']}();

?>

This is the same model/view/controller setup I use for all my sites, I find it easy to separate the code this way. So I'm using this controller in my API to call methods. If the method requires database access I pass it to the model.

 

I need direction on the API though, do I need one? If I use it to delete a user, how do I then display the resulting page after the user is deleted? Just do a PHP redirect?

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.