简体   繁体   中英

Laravel 5 Model Class not found

I am trying to port my web app from laravel 4 to 5 but I am having issues in that one of my model classes is not found.

I have tried to utilize namespacing and have the following my Models which are located locally C:\\xampp\\htdocs\\awsconfig\\app\\Models

the file which the error appears in looks like

<?php 
use App\Models\SecurityGroup;
function from_camel_case($input)

{
    preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
    $ret = $matches[0];
    foreach ($ret as &$match)
    {
        $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
    }
    return implode('_', $ret);
}
$resource_types = array();
$resource_types['AWS::EC2::Instance'] = 'EC2Instance';
$resource_types['AWS::EC2::NetworkInterface'] = 'EC2NetworkInterface';
$resource_types['AWS::EC2::VPC'] = 'VPC';
$resource_types['AWS::EC2::Volume'] = 'Volume';
$resource_types['AWS::EC2::SecurityGroup'] = 'SecurityGroup';
$resource_types['AWS::EC2::Subnet'] = 'Subnet';
$resource_types['AWS::EC2::RouteTable'] = 'RouteTable';
$resource_types['AWS::EC2::EIP'] = 'EIP';
$resource_types['AWS::EC2::NetworkAcl'] = 'NetworkAcl';
$resource_types['AWS::EC2::InternetGateway'] = 'InternetGateway';


 $accounts = DB::table('aws_account')->get();
 $account_list = array();




foreach(glob('../resources/sns messages/*.json') as $filename)
{
    //echo $filename;
    $data = file_get_contents($filename);

    if($data!=null)

    {


            $decoded=json_decode($data,true);  


            if(isset($decoded["Message"])) 
            { 
            //echo "found message<br>";
                $message= json_decode($decoded["Message"]);
if(isset($message->configurationItem))
{
//  echo"found cfi<br>";    
            $insert_array = array();
                $cfi = $message->configurationItem;
                switch ($cfi->configurationItemStatus)
                {

                    case "ResourceDiscovered":
                //echo"found Resource Discovered<br>";  
                        if (array_key_exists($cfi->resourceType,$resource_types))

                        {
                            //var_dump($cfi->resourceType);
                            $resource = new $resource_types[$cfi->resourceType];

                            foreach ($cfi->configuration as $key => $value)
                            {
                                if (in_array($key,$resource->fields))
                                {
                                    $insert_array[from_camel_case($key)] = $value;
                                }
                            }




                            $resource->populate($insert_array);
                            if (!$resource->checkExists())
                            {
                                $resource->save();

                                    if(isset($cfi->configuration->tags))
                                    {
                                       foreach ($cfi->configuration->tags as $t )

                                        {

                                        $tag= new Tag;
                                    $tag->resource_type = "instance";

                                    $tag->resource_id = $resource->id;
                                    $tag->key = $t->key;
                                    $tag->value = $t->value;

                                    $tag->save();


                                        /*if(isset($cfi->awsAccountId))
                                    {

                                     foreach ($accounts as $a)

                                     {
                                       $account_list = $a->account_id;
                                     }


                                    if (!in_array($account_id,$account_list))
                                    {



                                    $account_id = new Account;
                                    $account_id->aws_account_id = $cfi->awsAccountId;

                                    $account_list[] = $account_id;
                                     $account_id->save();

                                        }        






                                } */
                                }
                                }




                            }

                        }

                        else
                        {
                            echo "Creating ".$cfi["resourceType"]." not yet supported<br>";
                        }





                    break;
                    case 'ResourceDeleted':
                    //  echo"found Resource Deleted<br>";
                        //ITEM DELETED
                        if (array_key_exists($cfi->resourceType,$resource_types))
                        {
                            //var_dump($cfi->resourceType);
                                                        $resource = new $resource_types[$cfi->resourceType];



                            if ($resource->checkExists($cfi->resourceId))
                            {
                                $resource->delete();
                                  if( isset($cfi->configuration->tags))
                                      {
                                        foreach ($cfi->configuration->tags as $t )

                                            {
                                             $tag= new Tag;
                                              $tag->resource_type = "instance";

                                              $tag->resource_id = $resource->id;
                                              $tag->key = $t->key;
                                              $tag->value = $t->value;
                                              if ($tag->checkExists($cfi->configuration->tags))
                                              {
                                               $tag->delete();
                                              }
                                        }


                                     }

                        }
                        }
                        else
                        {
                            echo "Deleting ".$cfi["resourceType"]." not yet supported<br>";
                        }
                    break;
                    case 'OK':
                        //echo"found Resource OK<br>";
                        //ITEM UPDATED




                                               if (array_key_exists($cfi->resourceType, $resource_types))
                                                {
                            //var_dump($cfi->resourceType);
                                                        $resource = new $resource_types[$cfi->resourceType];

                                                        if ($resource->checkExists($cfi->resourceId))
                                                       {
                                                                foreach ($cfi->configuration as $key => $value)
                                                                {
                                                                        if (in_array($key,$resource->fields))
                                                                        {
                                                                                $update_array[from_camel_case($key)] = $value;
                                                                        }
                                                                }
                                $resource->populate($update_array);
                                                                $resource->save();
                                                        }        
                                                } 








                                                else
                                                {
                                                        echo "Updating ".$cfi["resourceType"]." not yet supported<br>";
                                                }
                    break;
                    default:
                        echo "Status ".$cfi['configurationItemStatus']." not yet supported<br>";
                    break;  

                    } 


            } 
        }
    }
}

and the corresponding model whose class cannot be found looks like :

<?php namespace App\Models;
use Eloquent;
class SecurityGroup extends Eloquent
{
    protected   $table      = 'security_group';
    public      $timestamps = false;
    protected   $guarded    = array('id');

    public      $fields     = array('groupId',
                                    'groupName',
                                    'description',
                                    'ownerId'

                        );


    public function checkExists()
    {
        return self::where('group_id', $this->group_id)->first();
    }

    public function populate($array)
    {
        foreach ($array as $k => $v)
        {
            $this->$k = $v;
        }
    }
}

I am lost as to what is causing the problem I don't see any typos but then again who knows as always thanks for any help given.

解决资源类型需要完整的名称空间声明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM