Jump to content
Larry Ullman's Book Forums

__Construct() Is Return True--> Is It True?


Recommended Posts


public function __construct() {
$this->verb = $_SERVER['REQUEST_METHOD'];
$this->url_elements = explode('/', $_SERVER['PATH_INFO']);
$this->parseIncomingParams();
        // initialise json as default format
        $this->format = 'json';
        if(isset($this->parameters['format'])) {
            $this->format = $this->parameters['format'];
        }
        return true;
    }

Hi ,

 

I remember that ___construct() didn't return any values..

 

On one of the tutorials on REst I have seen the code for creating a REst Server..

Can anyone please explain this?. How can they used it and any side effects?

 

Source of the code: http://www.lornajane.net/posts/2012/building-a-restful-php-server-understanding-the-request

 

Thanks in advance.

sharma

Link to comment
Share on other sites

If you read some of the comments, you'll see that the blog post has some inaccuracies. To cut it short, this is not really good code for you to learn from. You are absolutely right about return values in a constructor - it doesn't work.

 

The general principle of Rest is really good and one you should stick with. What it basically does is allowing the client to utilize any programming language to get a response from your server. I could be coding Ruby, PHP, Java, Python, whatever, and still be able to use something like a cURL-request to send requests and get responses from your server. :)

 

Good luck and ask if you have other questions.

Link to comment
Share on other sites

Hi Antonio,

 

I am working on REst API called tonic using mongoDB..

 

I am able to connect to db outside the class..


   	$con = new MongoClient();
        $db = $con->codesnippets;
        $collection = $db->snippets;

        //fetch a record
        $snippet = $collection->findOne(array("id" => 1));

        print_r($snippet);

When I put this in Rest API method its not working..and error is ....nothing

<?php

namespace Tyrell;

use Tonic\Resource,
    Tonic\Response,
    Tonic\ConditionException;

/**
 * This is a
 *
 * The @uri annotation routes requests that match that URL to this resource. Multiple
 * annotations allow this resource to match multiple URLs.
 *
 * @uri /snippets/:idhash
 */
class Snippet extends Resource
{

    /**
     * @method GET
     * @provides application/json
     */
    function display() {

        // Connect to mongodb
        $con = new \MongoClient();
        $db = $con->codesnippets;
        $collection = $db->snippets;

        //print $this->idhash;

        //fetch record based on id
        $snippet = $collection->findOne(array("id" => $this->idhash));

        print_r($snippet);
        exit;
        /*if(!is_null($snippet)){
            $snippetArray = array('id' => $snippet["id"],
                                  'title' => $snippet["title"],
                                  'description' => $snippet["description"],
                                  'links' => $snippet['links'],
                                  'files' => $snippet['files'],
                                  'dateCreated' => $snippet["date_created"],
                                  'lastModified' => $snippet["last_modified"],
                                  'views' => $snippet["views"]);
        }
*/

        return json_encode($snippetArray);

        //$ds = $this->container['dataStore'];
        //return json_encode($ds->fetch($this->id));
    }


}

Is it conflicting with name space ?

 

can you please help me?.. I don't have much namespace knowledge.

 

Thanks in advance,

sharma

Link to comment
Share on other sites

Thanks for your prompt response.

 

I am not getting errors. I am suspicious about there is no db connections from API?

https://github.com/peej/tonic

 

I checked the issues and the author is saying we need to write our own db wrapper. How do I do that?

https://github.com/peej/tonic/issues/1#issuecomment-457804

 

it seems I need to have more oop. Please show me some light. Bit scared of namespaces, adapters etc ..nail biting

 

Thanks,

sharma

Link to comment
Share on other sites

Hi ,

 

I made Tonic Rest application  working using RedBean ORM wrapper without class dependency "Pimple".

The real credit goes to "Slim" Rest Framework.

Now I am able to work with both REST API's.

Mean time I encountered with the concept/hands on CURL, JSON to really work with Total REST Cycle.

 

I am happy to help anyone on this. REST Cycle.

 

Its all started with this article..

http://www.huntlycameron.co.uk/2012/05/project-creating-a-restful-php-web-service-with-tonic/

 

Next to Slim..

http://www.sitepoint.com/writing-a-restful-web-service-with-slim/

http://www.slimframework.com

 

 

Next to ..

http://www.ibm.com/developerworks/opensource/library/x-slim-rest/index.html

 

in the middle I got to know about..Pimple..

http://www.sitepoint.com/dependency-injection-with-pimple/

http://net.tutsplus.com/tutorials/php/dependency-injection-huh/

 

 

Finally End up

Redbean ORM, CURL, 

http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

 

 

The Book for PHP REST..

PHP Web Services on OReilly.com(Just 124 pages..)

 

 

Thanks for your support..

sharma chelluri

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...