Web Service and application

I just create the project in phalcon and I want to write REST base api's as well as website then how can I setup this environment.



29.9k

Hi,

have a look at that tutorial

But this only regarding services, I have to create application show to visitor and also the services for mobile device.

Is there any example that explains both the api and application into one.



29.9k

with phalcon, you write the server side application that will listen to clients requests and send responses (see link above). To "consume" this API, you will have to create the client side app, wich will interact with the server using ajax request: so a static html file with a bunch of javascript is what you need.

Some javascript framework are good choices for handling REST API client side. Emberjs is my favorite as it implements natively JSONAPI format and use the mvc application structure

Ya but in phalcon how I write api for mobile apps as well as the website to run in a browesers into the one Project.



29.9k

Ya but in phalcon how I write api for mobile apps as well as the website to run in a browesers into the one Project.

You write API as described in the tutorial. Writing apps that consumes this API for mobile devices (android, iOs ...) or as classical website is not phalcon's specific.

Then what is the basic use of phalcon, I mean when to use phalcon. Is it not good for api and website.

Ya but in phalcon how I write api for mobile apps as well as the website to run in a browesers into the one Project.

You write API as described in the tutorial. Writing apps that consumes this API for mobile devices (android, iOs ...) or as classical website is not phalcon's specific.



29.9k

here's an example of a basic json api:

<?php

// Use Loader() to autoload our model
$loader = new \Phalcon\Loader();

$loader->registerDirs(array(
    __DIR__ . '/models/'
))->register();

$di = new \Phalcon\DI\FactoryDefault();

//Set up the database service
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "blog",
        "password" => "********",
        "dbname" => "blog"
    ));
});

//Create and bind the DI to the application
$app = new \Phalcon\Mvc\Micro($di);

//Retrieves all post
$app->get('/post', function() use ($app){
    $phql = "SELECT * FROM Post ORDER BY title";
    $posts = $app->modelsManager->executeQuery($phql);

    $data = array();
    foreach ($posts as $post) {
        $data[] = array(
            'id' => $post->id,
            'title' => $post->title,
            'slug'=> $post->slug
            'date' => $post->date,
            'author' => $post->author_id,
            'category' => $pst->category_id,
            'excerpt' => $post-> excerpt,
            'body' => $post->body
        );
    }

    echo json_encode($data);
});


//Retrieves post based on slug
$app->get('/post/{slug}', function($slug)  use ($app){
       $query  = $app->modelsManager->createQuery("SELECT * FROM Cars WHERE slug = :slug:");
       $post   = $query->execute(array(
            'slug' => $slug
        ));

    echo json_encode($post);
});

//Adds a new post
$app->post('/post', function() {
// some code 
});

//Updates post based on primary key
$app->put('/post/{id:[0-9]+}', function() {
// some code
});

//Deletes post based on primary key
$app->delete('/post/{id:[0-9]+}', function() {
// some code
});

$app->handle();