Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组到字符串转换错误-细长细枝-_Php_Twig_Slim - Fatal编程技术网

Php 数组到字符串转换错误-细长细枝-

Php 数组到字符串转换错误-细长细枝-,php,twig,slim,Php,Twig,Slim,设置:我有一个超薄的应用程序,我拉在照亮DB和细枝视图 if (!$validator->passed()) { $errors = $validator->errors(); $users = User::all(); return $this->view($response, 'auth.login', compact('errors','users')); } 问题:当我运行上述代码时,我能够在视图中检索users

设置:我有一个超薄的应用程序,我拉在照亮DB和细枝视图

if (!$validator->passed()) {
        $errors = $validator->errors();
        $users = User::all();
        return $this->view($response, 'auth.login', compact('errors','users'));
    }
问题:当我运行上述代码时,我能够在视图中检索users变量,但是errors变量抛出以下错误

注意:/Users/davidchen/Documents/sites/slim.com/vendor/twig/twig/lib/twig/Environment.php(378):eval()d代码中的数组到字符串转换 在线 70阵列

errors变量返回一个多维数组,下面是我从print\r($errors)得到的结果

以下是我的相关项目文件:

  • 细枝设置文件(app.php)
  • 验证程序类
  • 身份验证控制器
  • login.twig文件
  • login.twig文件


    希望你们中的一位能对这个问题有所帮助。我整个上午都在为这件事挣扎

    您可以尝试按顺序循环每个项目。例如,要显示名为users的变量中提供的用户列表,请执行以下操作:

    <h1>Members</h1>
    <ul>
        {% for user in users %}
            <li>{{ user.username|e }}</li>
        {% endfor %}
    </ul>
    
    成员
    
      {users%%中的用户为%s}
    • {{user.username | e}}
    • {%endfor%}

    当运行
    $this->view()
    方法时,调用
    compact()
    时,您想做什么?不要将代码作为图像发布。我正在尝试将包含数据集合的命名变量数组传递给我的视图。另一种方法是将compact替换为['variable_name'=>object/array]。这一行:
    {{errors}
    在您的细枝模板中基本上是:
    echo$errors
    。由于您的
    $errors
    -变量是一个数组,因此无法回显它,您将收到“数组到字符串转换”通知。所以基本上,不要回显数组。@Magnus Eriksson:谢谢,我的问题是,我是否为我的{{{users}返回json对象。
    $c = $app->getContainer();
    
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($config['config']['db']);
    $capsule->setAsGlobal();
    $capsule->bootEloquent();
    
    $c['db'] = function($c) use ($capsule){
        return $capsule;
    };
    
    $c['view'] = function($c){
        $options['cache']=false;
        $view = new \Slim\Views\Twig(__DIR__."/../views", $options);
        $view->addExtension(new \Slim\Views\TwigExtension(
            $c->router,
            $c->request->getUri()
        ));
        $view->getEnvironment()->addGlobal('flash', $c->flash);
        return $view;
    };
    
    $c['flash'] = function($c){
        return new Slim\Flash\Messages();
    };
    
    namespace App\Models\Auth;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Capsule\Manager as DB;
    use Carbon\Carbon;
    use DateTime;
    
    
    class Validator extends Model
    {
        protected   $field_name,
                    $data,
                    $errors = [];
    
    /*
    * Main validator
    */
    
        public function __construct($request, $fields = []){
            $data = $request->getParams();
            $this->data = $data;
            foreach ($fields as $field => $constraints) {
                $this->field_name = $field;
                if (isset($data[$field])) {
                    $field_value = $data[$field];
                    foreach (explode("|", $constraints) as  $constraint) {
                        $obj = explode(":", $constraint);
                        $function_name = $obj[0];
                        if (isset($obj[1])) {
                            if(method_exists($this, $function_name))
                            {
                                $this->$function_name($obj[1],$field_value);
                            }
                        }
                    }
                }else{
                    if (strpos($constraints, 'required') !== false) {
                        $validator->report($validator->field_name.' field is requried');
                    }   
                }
            }
            return $this;
        }
    /*
    * Object Interface Methods
    */
        private function report($message){
            $this->errors[$this->field_name][]= $message;
        }
    
        public function errors(){
    
            return $this->errors;
        }
    
        public function passed(){
            if (!count($this->errors)) {
                return true;
            }
        }
    
    /*
    * Validation Rules
    */
    
        public function max($length,$value){
            if (strlen($value)>$length) {
                $this->report("{$this->field_name} must consist of less than {$length} characters");
            }
        }
    
        public function min($length,$value){
            if (strlen($value)<$length) {
                $this->report("{$this->field_name} must consist of atleast {$length} characters");
            }
        }
    
        public function distinct($tableName,$value){
            if (DB::table($tableName)->where($this->field_name, $value)->exists()) {
                $this->report("{$this->field_name} already exists");
            }
        }
    
        public function date($format,$date){     
    
            if (!preg_match("/\d{4}-\d{2}-\d{2}\b/",$date)) {
                $this->report("incorrect {$this->field_name} values");
            }else{
                $d = DateTime::createFromFormat($format, $date); 
                if ($d && $d->format($format) !== $date) {
                    $this->report("{$this->field_name} format should be {$format}");
                }
            }        
        }
    
        public function match($matchField,$value){
            if (isset($this->data[$matchField])) {
                $valueTwo = $this->data[$matchField];
                if ($value !== $valueTwo) {
                    $this->report("{$this->field_name} does not match {$matchField}");
                }
            }else{
                $this->report("{$matchField} is required");
            }
    
    
        }
    
        public function format($type,$value){
            switch ($type) {
                case 'noWhiteSpace':
                    if (preg_match("/\s/",$value)) {
                        $this->report("{$this->field_name} may not contain any spaces");
                }break;
    
                case 'alpha':
                    if (preg_match("/[^a-zA-Z]/",$value)) {
                        $this->report("{$this->field_name} may only contain letters");
                }break;
    
                case 'alphaNum':
                    if (preg_match("/[^a-zA-Z0-9]/",$value)) {
                        $this->report("{$this->field_name} may only contain letters");
                }break;
    
                case 'email':
                    if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
                        $this->report("in correct {$this->field_name} format");
                }break;
    
                default:
                    # code...
                    break;
            }
        }
    
    }
    
    namespace App\Controllers;
    /**
    * 
    */
    class Controller 
    {
        protected $c;
        function __construct($container)
        {
            $this->c = $container;
        }
    
        public function view($response, $path,$variables = []){
            $this->c->view->render($response, str_replace(".","/",$path).".twig", $variables);
        }
    
        public function pathFor($routeName,$data = []){
            return $this->c->router->pathFor($routeName,$data);
        }
    }
    
    namespace App\Controllers\Auth;
    use App\Models\User\User;
    use App\Controllers\Controller;
    use App\Models\Auth\Validator;
    /**
    * 
    */
    class AuthController extends Controller
    {
        public function getLogin($request, $response){
            return $this->view($response, 'auth.login');
        }
    
        public function postLogin($request, $response){
            $validator =  new Validator($request,[
                'username'=>'required|min:3|max:64|format:alphaNum|distinct:users',
                'password'=>'required|min:6|max:64|',
            ]);
    
            if (!$validator->passed()) {
                $errors = $validator->errors();
                $users = User::all();
                return $this->view($response, 'auth.login', compact('errors','users'));
            }
            return $this->view($response, 'home.login');
        }
    }
    
    <h1>Members</h1>
    <ul>
        {% for user in users %}
            <li>{{ user.username|e }}</li>
        {% endfor %}
    </ul>