Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

json - A PHP Error was encountered Undefined: Property format for ApiHandler class | Codeigniter REST API | Backbone.js

I am trying to build a Wishlist application with a REST API and Backbone.js. I'm having trouble with the routes. I have an ApiHandler Controller in controllers/api. I'm getting the following error when trying to display the view.

A PHP Error was encountered
Severity: Notice

Message: Undefined property: ApiHandler::$format

Filename: libraries/REST_Controller.php

Line Number: 798

Backtrace:

File: C:xampphtdocs2017464WishList-AppapplicationlibrariesREST_Controller.php
Line: 798
Function: _error_handler

File: C:xampphtdocs2017464WishList-AppapplicationlibrariesREST_Controller.php
Line: 703
Function: response

File: C:xampphtdocs2017464WishList-Appindex.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined property: ApiHandler::$format

Filename: libraries/REST_Controller.php

Line Number: 816

Backtrace:

File: C:xampphtdocs2017464WishList-AppapplicationlibrariesREST_Controller.php
Line: 816
Function: _error_handler

File: C:xampphtdocs2017464WishList-AppapplicationlibrariesREST_Controller.php
Line: 703
Function: response

File: C:xampphtdocs2017464WishList-Appindex.php
Line: 315
Function: require_once

An uncaught Exception was encountered
Type: Error

Message: Call to a member function factory() on null

Filename: C:xampphtdocs2017464WishList-AppapplicationlibrariesREST_Controller.php

Line Number: 816

Backtrace:

File: C:xampphtdocs2017464WishList-AppapplicationlibrariesREST_Controller.php
Line: 703
Function: response

File: C:xampphtdocs2017464WishList-Appindex.php
Line: 315
Function: require_once

This is my Controller ApiHandler class

<?php defined('BASEPATH') OR exit('No direct script access allowed');
use RestserverLibrariesREST_Controller;
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/Format.php';

class ApiHandler extends RestserverLibrariesREST_Controller
{

    public function __construct()
    {
        parent::__construct();
        //Load User Model
        $this->load->model('user_model','UserModel');
    }
public function register_post(){
        header("Access-Control-Allow-Origin: *");
        $data = array("returned");
        $this->response($data);
        // XSS Filtering (https://www.codeigniter.com/user_guide/libraries/security.html)
        $_POST = $this->security->xss_clean($_POST);

        $this->form_validation->set_data([
            'full_name'=>$this->post('full_name',TRUE),
            'username'=>$this->post('username',TRUE),
            'email'=>$this->post('email',TRUE),
            'password'=>$this->post('password',TRUE),
            'wishlist_name'=>$this->post('wishlist_name',TRUE),
            'wishlist_description'=>$this->post('wishlist_description',TRUE)
        ]);

        //Form Validation
        $this->form_validation->set_rules('full_name','Full Name','trim|required|maxlength[50');
        $this->form_validation->set_rules('username','Username','trim|required|is_unique[users.username]|max_length[20]',array('is_unique'=>'This %s already exists please enter another username'));
        $this->form_validation->set_rules('email','Email','trim|required|valid_email|max_length[80]',array('is_unique'=>'This %s already exists, please enter another email address.'));
        $this->form_validation->set_rules('password','Password','trim|required|max_length[100]');
        $this->form_validation->set_rules('wishlist_name','Wishlist Name','trim|required|max_length[100]');
        $this->form_validation->set_rules('wishlist_description','Wishlist Description','trim|required|max_length[1000]');

        if ($this->form_validation->run() == FALSE){
            //Form Validation
            $message = array(
                'status'=>FALSE,
                'error'=>$this->form_validation->error_array(),
                'message'=>validation_errors()
            );
            $this->response($message,REST_Controller::HTTP_NOT_FOUND);

        }else{
            //inserting data
            $insert_data = [
                'full_name'=>$this->post('full_name',TRUE),
                'email'=>$this->post('email',TRUE),
                'username'=>$this->post('username',TRUE),
                'password'=>password_hash($this->post('password',TRUE)),
                'wishlist_name'=>$this->post('wishlist_name',TRUE),
                'wishlist_description'=>$this->post('wishlist_description',TRUE)
            ];

            //Insert the User into the Database
            $output = $this->UserModel->insert_user($insert_data);
            if ($output>0 AND !empty($output)){
                //Send Success code 200
                $message = [
                    'status'=>TRUE,
                    'message'=>"User registration successful"
                ];
                $this->response($message,REST_Controller::HTTP_OK);
            }else{
                //Error
                $message = [
                    'status'=>FALSE,
                    'message'=>"Account not registered"
                ];
                $this->response($message,REST_Controller::HTTP_NOT_FOUND);
            }
        }
    }

    /** User Login API
     * -------------------------
     * @param:username or email
     * @param: password
     * -------------------------
     * @method:POST
     * @link:api/user/login
     */
    public function login_post(){
        header("Access-Control-Allow-Origin:*");

        // XSS Filtering (https://www.codeigniter.com/user_guide/libraries/security.html)
        $_POST = $this->security->xss_clean($_POST);

        //Form Validation
        $this->form_validation->set_rules('username','Username','trim|required');
        $this->form_validation->set_rules('password','Password','trim|required|maxlength[100]');
        if($this->form_validation->run()==FALSE){
            //Form Validation Errors
            $message = array(
                'status'=> FALSE,
                'error'=>$this->form_validation->error_array(),
                'message'=>validation_errors()
            );

            $this->response($message,REST_Controller::HTTP_NOT_FOUND);
        }else{
            //Load the Login function
            $output = $this->user_login($this->post('username'),$this->post('password'));
            if (!empty($output)AND $output!= FALSE){
                //Load the Authorization Library
                $this->load->library('Authorization_Token');

                //Generate Token
                $token_data['id'] = $output->id;
                $token_data['full_name'] = $output->full_name;
                $token_data['username'] =$output->username;
                $token_data['email'] = $output->email;
                $token_data['time'] = time();

                $user_token = $this->authorization_token->generateToken($token_data);

                $return_data = [
                    'user_id'=>$output->id,
                    'full_name'=>$output->full_name,
                    'email'=>$output->email,
                    'wishlist_name'=>$output->wishlist_name,
                    'wishlist_description'=>wishlist_description,
                    'token'=>$user_token
                ];

                //Login Success
                $message = [
                    'status'=>TRUE,
                    'data'=>$return_data,
                    'message'=>"User Login Successful"
                ];
                $this->response($message,REST_Controller::HTTP_OK);

            }else{
                //Login error
                $message = [
                    'status'=>FALSE,
                    'message'=>"Invalid Username or Password"
                ];
                $this->response($message,REST_Controller::HTTP_NOT_FOUND);
            }
        }
    }
}

The view includes all the pages to be displayed registration and also login Wishlist_item

<!doctype html>
<html lang="en">
<head>
    <meta charset ="utf-8">
    <title>WISH LIST</title>
    <!--favicon-->
    <link rel="icon" type="image/png" href="<?php echo base_url();?>assets/images/favicon.png">

    <!--CSS -->
    <link rel="stylesheet" href="<?php echo base_url();?>assets/css/font-awesome.min.css">
    <link href="<?php echo base_url();?>assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?php echo base_url();?>assets/css/mdb.min.css" rel="stylesheet">

</head>
<body style="background-image: url('<?php echo base_url();?>assets/images/cover.png')">
    <div class="container">
        <!--Wish List-->
        <hr>
        <div class="newItem"></div>
        <div class="page"></div>
    </div>

    <!-- Javascript dependencies and Libraries-->
    <script src="<?php echo base_url();?>assets/js/jquery.min.js" type="text/javascript"></script>
    <script src="<?php echo base_url();?>assets/js/underscore-min.js" type="text/javascript"></script>
    <script src="<?php echo base_url();?>assets/js/backbone-min.js"></script>
    <script src="<?php echo base_url();?>assets/js/sweetalert.min.js"></script>
    <script type="text/javascript" src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<?php echo base_url();?>assets/js/mdb.min.js"></script>

    <!--Load the backbone models-->
    <script src="<?php echo base_url();?>assets/scripts/models/UserModel.js"></script>
    <script src="<?php echo base_url();?>assets/scripts/models/ItemModel.js"></script>

    <!--load the backbone collections-->
    <script src="<?php echo base_url();?>assets/scripts/collections/WishlistCollection.js"></script>

    <!--load the backbone views-->
    <script src="<?php echo base_url();?>assets/scripts/views/LoginView.js"></script>
    <script src="<?php echo base_url();?>asserts/scripts/views/SignupView.js"></script>
    <script src="<?php echo base_url();?>assets/scripts/views/WishListView.js"></script>
    <script src="<?php echo base_url();?>assets/scripts/views/ShareListView.js"></script>

    <!--template for user login view-->
    <script type="text/template" id="user-login-template">
        <div class="container-fluid">
            <div style="text-align:center">
<!-- <img  style="width: 650px;" src="--><?php //echo base_url(); ?><!--assets/images/cover.png" >-->
            </div>
            <div style='justify-content: center;' class="row">
                <div style="" class="co

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...