The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Using Slim Framework

Sundar Machamasi's Avatar

Sundar Machamasi

07 Sep, 2015 07:34 AM

I need help in slim framework

  1. 1 Posted by Sundar Machamas... on 07 Sep, 2015 07:36 AM

    Sundar Machamasi's Avatar

    Hi I am sundar,
    I need to use json_encode to display result. Can u help me out?

  2. Support Staff 2 Posted by Andrew Smith on 10 Sep, 2015 08:24 AM

    Andrew Smith's Avatar

    You have not posted what you need help with, can you post some code so we can help where possible?

  3. 3 Posted by Sundar Machamas... on 10 Sep, 2015 08:27 AM

    Sundar Machamasi's Avatar

    *i need to change json format : my code is like this *
    <?php
    require_once '../Slim/Slim.php';
    require_once '../db.php';

    $app = new Slim();

    $app->get('/users', 'getUsers');

    $app->run();

    // Show All Users
    function getUsers(){
    $sql = "SELECT * FROM tbl_user ORDER BY user_id";
    try{
    $db = getConnection();
    $stmt = $db->query($sql);
    $users = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo '{"AllUsers":'. json_encode($users).'}';
    }catch(PDOException $e){
    echo '{"error":'. $e->getMessage().'}}';
    }
    }

  4. Support Staff 4 Posted by Andrew Smith on 10 Sep, 2015 08:46 AM

    Andrew Smith's Avatar

    But this has no relation to Slim itself, you are basically writing the code incorrectly.

    echo json_encode(array('AllUsers' => $users));
    

    That is what you need, you should probably take a look at a few tutorials online about creating json from an array.

  5. 5 Posted by Sundar Machamas... on 10 Sep, 2015 10:58 AM

    Sundar Machamasi's Avatar

    i need to add user info by passing parameters through url : Plz help me out
    of this
    here is my code:
    #index.php
    <?php
    require_once '../Slim/Slim.php';
    require_once '../db.php';

    $app = new Slim();

    $app->post('/create', 'addUser'); // Add user

    $app->run();

    // Add User
    function addUser() {
    error_log('addUser\n', 3, '/var/tmp/php.log');
    $request = Slim::getInstance()->request();
    $user = json_decode($request->getBody());
    $sql = "INSERT INTO tbl_user (username, password, address, phone) VALUES
    (:username, :password, :address, :phone)";
    try {
    $db = getConnection();
    $stmt = $db->prepare($sql);
    $stmt->bindParam("username", $user->username);
    $stmt->bindParam("password", $user->password);
    $stmt->bindParam("address", $user->address);
    $stmt->bindParam("phone", $user->phone);
    $stmt->execute();
    $user->user_id = $db->lastInsertId();
    $db = null;
    echo json_encode($user);
    } catch(PDOException $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
    }
    ?>

    #db.php
    <?php
    function getConnection(){
    $dbhost="localhost";
    // For Local Server
    $dbuser="root";
    $dbpass="";
    $dbname="db_slim";
    $dbh = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
    }
    ?>

    #url:
    http://localhost/IDEA_PROJECTS/2015/SLIM_FRAMEWORKS/apitest/v1/create/newuser/newpass/newaddress/newphone

    #output:
    404 Page Not Found

    The page you are looking for could not be found. Check the address bar to
    ensure your URL is spelled correctly. If all else fails, you can visit our
    home page at the link below.

  6. 6 Posted by Sundar Machamas... on 10 Sep, 2015 11:04 AM

    Sundar Machamasi's Avatar

    i need to show different JSON format using slim framework . plz sombody post example link related to that.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac