carlos.ashh's avatar

Error: Class 'App\Http\Controllers\config' not found

I have created a constant file in config directory. file name:databsehost.php file name:databsehost.php

0 likes
16 replies
tykus's avatar

You can use the config() helper function or the facade Config::get(). If you use the facade, remember to use Config; at the top of your controller.

1 like
carlos.ashh's avatar

could you please show me how to add it because I have added like this way namespace App\Http\Controllers; namespace config::get();

it gives an error

i used another way:

use config();

this also gives error

tykus's avatar

Because they both are incorrect:

<?php

namespace App\Http\Controllers;

use Config;

class ThingController extends Controller
{
    public function whatever()
    {
        $var = Config::get('databsehost.const');
    
        // or, if you used the helper, you don;t need the `use` statement:
        // $var = config('databsehost.const');
    }
}
1 like
tykus's avatar

This error is unrelated to the original question; just because you are not explicitly using cURL, does not mean that somewhere in a package it is not being used.

What are you getting back from the config file, and what are you doing with it. You will need to share some code

1 like
carlos.ashh's avatar

I am just retrieving data from couch db. here I will share my full code

public function getdata() {

    $content =null;
    
    try { 
            $client = new Client();                             
            
            //$apiRequest = $client->request('GET', 'http://localhost:5984/user/_design/userdesign/_view/user-view?limit=20&reduce=false');
            
            $url=config::get('databasehost.couchdbhost.couchdbport');
            //print_r($url);
            $apiRequest = $client->request('GET',$url);
            
        
        $code = $apiRequest->getBody()->getContents();
 
    } catch (RequestException $re) {
      //For handling exception
     return $re->error;
    }
    return $code;
    //return response()->json($code);
}

In the above coce:

$apiRequest = $client->request('GET', 'http://localhost:5984/user/_design/userdesign/_view/user-view?limit=20&reduce=false'); is working fine. its fetching the data and displaying it.

But now I want to use using constant file so I created a file in config

file name: databasehost.php

so how can I retrieve data using config constant file. please show me full code like what I have to write in below code to fetch the data form couchDB

$url=config::get('databasehost.couchdbhost.couchdbport'...

$apiRequest = $client->request('GET',$url);

tykus's avatar

What do you get whenever you dd($url) after fetching it from Config?

carlos.ashh's avatar

below is databasehost.php file in config

[   
    'couchdbhost' => '127.0.0.1',
    'couchdbport' => 5984
];

and using this config as constant file, I want to fetch the records from database by using API

I use couchDB

below is I was successfully fetching the data without using config(constant file)

public function getguzzle() {

    $content =null;
    
    try { 
            $client = new Client();                             
            
            $apiRequest = $client->request('GET', 'http://localhost:5984/user/_design/userdesign/_view/user-view?limit=20&reduce=false');
            
            //$url=config::get('
            //echo $url;
            //print_r($url);
            //return response()->json($url);
            

        $apiRequest = $client->request('GET',$url);
            
        
        $code = $apiRequest->getBody()->getContents();
 
    } catch (RequestException $re) {
      //For handling exception
     return $re->error;
    }
    return $code;
    //return response()->json($code);
}

This is working fine without using config

Now I want to do using constant file. how can I do using config..

tykus's avatar

That's not what I asked you, but anyway... this will not work:

$url=config::get('databasehost.couchdbhost.couchdbport');

because it suggests a structure like this:

//databasehost.php
[
    'couchdbhost' => [
        'couchdbport' => //value
    ]
]

You are not storing the full url in the config file either; where are you getting the remaining segments of the url /user/_design/userdesign/_view/user-view?limit=20&reduce=false?

public function getguzzle()
{
    $content =null;

    $host =  config('databasehost.couchdbhost') . ':' . config('databasehost.couchdbport');
    $url = $host . '/user/_design/userdesign/_view/user-view?limit=20&reduce=false';

    try { 
        $client = new Client();
        $apiRequest = $client->request('GET', $url);
            $code = $apiRequest->getBody()->getContents();
 
        } catch (RequestException $re) {
            //For handling exception
    }

    return response()->json($code);
}
1 like
carlos.ashh's avatar

I did same whatever u gave but i get this error:

ConnectException in CurlFactory.php line 186: cURL error 7: Failed to connect to port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

//databasehost.php

[   
    'connections' => [
        'couchdbhost' => '127.0.0.1',
        'couchdbport' => 5984,
        
    ]
];

could you please tel me on top of controller what I have to add

tykus's avatar

Why did you change the config structure? How are you getting the config data now?

carlos.ashh's avatar

where can I put the localhost adds(127.0.01)

where can I put my localhost adds and port (127.0.0.1 and 5984)

tykus's avatar
tykus
Best Answer
Level 104

@carlos.ashh I have given you the answer to your question already, but you are changing the structure of the config file without understanding the consequences; I am going to be very explicit here:

//databasehost.php
<?php

return [   
    'connections' => [
        'couchdbhost' => '127.0.0.1',
        'couchdbport' => 5984,
        
    ]
];

You can access these values using

config('databasehost.connections.couchdbhost'); // 127.0.0.1
// and
config('databasehost.connections.couchdbport'); // 5984

// OR using Facades

Config::get('databasehost.connections.couchdbhost');
// and
Config::get('databasehost.connections.couchdbport');

Can you see the relationship between parameter to config() and the config filename and the structure of the array therein with??

config('databasehost.connections.couchdbhost');
//     | filename   | first key | second key
1 like
carlos.ashh's avatar

Thank you. It worked. Thank you so much..

Actually I am beginner. I am a student. I dint know how to use it.

carlos.ashh's avatar

So now I have done the retrieval part.

Can you show me how can I Insert, Update and delete the data.

it would be helpful for me if you show other crud operation.

Reejesh's avatar

Dont forget to include use Illuminate\Support\Facades\Config;

1 like

Please or to participate in this conversation.