Реализация Ext.direct с помощью laravel - PullRequest
0 голосов
/ 01 октября 2019

Я пытаюсь реализовать связь ext.direct между Extjs 7 (пробная версия) и Laravel 6. Я ничего не знаю о ext.direct, поэтому я попробовал следовать официальному руководству по документации sencha https://docs.sencha.com/extjs/7.0.0/guides/backend_connectors/direct/specification.html I 'Я пытаюсь сделать логин, но я не могу заставить его работать. Моя структура папок:

Laravel project
--app
----Http
-------Controllers
--------Auth
----------LoginController
--public
----Extjs Files
----api
------Ext.direct files (api.php,router.php,config.php)

Мой api.php:

<?php
 require('config.php');
 header('Content-Type: text/javascript');

 // convert API config to Ext Direct spec
 $API = get_extdirect_api();
 $actions = array();

 foreach ($API as $aname=>&$a) {
    $methods = array();
    foreach ($a['methods'] as $mname=>&$m) {
        if (isset($m['len'])) {
            $md = array(
                'name'=>$mname,
                'len'=>$m['len']
            );
        } else {
            $md = array(
                'name'=>$mname,
                'params'=>$m['params']
            );
        }
        if (isset($m['formHandler']) && $m['formHandler']) {
            $md['formHandler'] = true;
        }
        $methods[] = $md;
    }
    $actions[$aname] = $methods;
 }

 $cfg = array(
    'url'=>'api/router.php',
    'type'=>'remoting',
    'actions'=>$actions
 );
 echo 'var Ext = Ext || {}; Ext.REMOTING_API = ';

 echo json_encode($cfg);
 echo ';';

Мой router.php

<?php
 require('config.php');

 class BogusAction {
    public $action;
    public $method;
    public $data;
    public $tid;
 }

 $isForm = false;
 $isUpload = false;
 if (isset($HTTP_RAW_POST_DATA)) {
    header('Content-Type: text/javascript');
    $data = json_decode($HTTP_RAW_POST_DATA);
 }
 else if(isset($_POST['extAction'])){ // form post
    $isForm = true;
    $isUpload = $_POST['extUpload'] == 'true';
    $data = new BogusAction();
    $data->action = $_POST['extAction'];
    $data->method = $_POST['extMethod'];
    $data->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null;
    $data->data = array($_POST, $_FILES);
 }
 else {
    die('Invalid request.');
 }

 function doRpc($cdata){
    $API = get_extdirect_api('router');

    try {
        if (!isset($API[$cdata->action])) {
            throw new Exception('Call to undefined action: ' . $cdata->action);
        }

        $action = $cdata->action;
        $a = $API[$action];

        $method = $cdata->method;
        $mdef = $a['methods'][$method];

        if (!$mdef){
            throw new Exception("Call to undefined method: $method " .
                                "in action $action");
        }

        $r = array(
            'type'=>'rpc',
            'tid'=>$cdata->tid,
            'action'=>$action,
            'method'=>$method
        );

        //require_once("classes/$action.php");
        //$o = new $action();

        if (isset($mdef['len'])) {
            $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array();
        }
 else {
            $params = array($cdata->data);
        }

        array_push($params, $cdata->metadata);

        $r['result'] = call_user_func_array(array($o, $method), $params);
    }

    catch(Exception $e){
        $r['type'] = 'exception';
        $r['message'] = $e->getMessage();
        $r['where'] = $e->getTraceAsString();
    }

    return $r;
 }

 $response = null;

 if (is_array($data)) {
    $response = array();
    foreach($data as $d){
        $response[] = doRpc($d);
    }
 }
 else{
    $response = doRpc($data);
 }

 if ($isForm && $isUpload){
    echo '<html><body><textarea>';
    echo json_encode($response);
    echo '</textarea></body></html>';
 }
 else{
    echo json_encode($response);
 }

И мой config.php

<?php

function get_extdirect_api() {

   $API = array(
       'LoginController' => array(
           'methods' => array(
               'login' => array(
                   'len' => 1,
                   'params' => array($username,$password),
                   'formHandler' => true
               )
           )
       )
   );

   return $API;
}

loginForm:

items: {
        xtype: 'form',
        reference: 'form',
        api: {
            submit: 'LoginController.login'
        },
        paramOrder: ['username','password'],
        items: [{
            xtype: 'textfield',
            name: 'username',
            fieldLabel: 'Username',
            allowBlank: false
        }, {
            xtype: 'textfield',
            name: 'password',
            inputType: 'password',
            fieldLabel: 'Password',
            allowBlank: false
        }],
        buttons: [{
            text: 'Login',
            formBind: true,
            handler: 'onLoginClick'
        }]
    }

onLoginClicK:

onLoginClick: function() {
        // This would be the ideal location to verify the user's credentials via
        // a server-side lookup. We'll just move forward for the sake of this example.
        form = this.getView().down('form').getForm();
        values = form.getValues();
        form.submit({
            params: {
                username: values.username,
                password: values.password
            },
            success : function(){
                console.log('success');
            },
            failure : function(){
                console.log('failure');
            },
            callback: function(){
                console.log('callback');
            }
        });
        // Set the localStorage value to true
        sessionStorage.setItem("loggedIn", true);
        // Remove Login Window
        this.getView().destroy();

        // Add the main view to the viewport
        Ext.create({
            xtype: 'mainview'
        });

    }

В данный момент похоже, что он даже не достигает LoginController, но файл api.php загружаетсяправильно

...