How to properly load custom package in Laravel? - php

How to load Facebook class or any other non Laravel package in my controllers in Laravel 5. I have installed Facebok SDK - in my composer.json:
"facebook/php-sdk-v4" : "~5.0"
In my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Facebook;
class MyController extends Controller
{
public function index()
{
// this fails
$fb = new Facebook();
}
}
This generates error:
Class 'App\Http\Controllers\Facebook' not found
How to load Facebook package or any other custom packages in my controllers?

OK, I solved it here is the correct way to instantiate a Facebook class:
Wrong:
$fb = new Facebook(....)
Correct:
$fb = new Facebook\Facebook(....)

Related

Why can't i use Illuminate\Support\Facades\Http class in fetching data in laravel using Guzzle

Am new in consuming api. am trying to fecth data from https://jsonplaceholder.typicode.com/posts in Laravel but am getting error related to class not imported even though i have imported the Http Class used in the Laravel Docs.
this is my controller ApiController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class ApiController extends Controller
{
public function fetch()
{
$response = Http::get('https://jsonplaceholder.typicode.com/posts');
dd($response);
}
}
this is the error am receiving
"Class 'Illuminate\Support\Facades\Http' not found"
I have already installed the installed the Guzzle package. What am i doing wrong please.
You seem to be using the Laravel 7 way of using Guzzle. Change your Controller like so to make it work in Laravel 5.8
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
class ApiController extends Controller
{
public function fetch()
{
$client = new Client;
$request = $client->get('https://jsonplaceholder.typicode.com/posts');
$response = $request->getBody();
dd($response);
}
}
Edit: To get the contents of the request use dd($response->getContents()); instead of dd($response);

Model Class not found error

Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.

Custom helper: class not found. Laravel 5.1

I've created a custom new file, app/Http/Helpers.php and added:
<?php
namespace app\Http;
class ConnectionsHelper {
public static function organisation($id) {
return 'ID:'.$id;
}
}
In Composer.json, in the autoload-block I've added:
"files": [
"app/Http/Helpers.php"
]
And then I ran "composer dump-autoload".
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class myController extends Controller
{
public function index()
{
echo ConnectionsHelper::organisation(2);
}
}
And get in return:
FatalErrorException in OrganisationsController.php:
Class 'App\Http\Controllers\ConnectionsHelper' not found
You need to provide a namespace alias in your controller.
use App\Http\ConnectionsHelper
Autoloading a file does not mean that the classes in that file are required/included in all other scripts in the app. It just means that you are making those files available to your app. In this case, your helper file is already inside the App namespace which is autoloaded by default, so you can remove the files bit of your composer.json completely.

How to set up laravel 5 with facebook

How to set up login with facebook with Laravel 5 I installed php-sdk-v4 and tried something like this:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use FacebookHelper;
use Illuminate\Support\Facades\Config;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
class LoginFacebookController extends Controller {
private $helper;
public function _construct(){
FacebookSession::setDefaultApplication(Config::get('facebook.app_id'), Config::get('facebook.app_secret'));
$this-> helper = new FacebookRedirectLoginHelper(url('login/fb/callback'));
}
public function getUrlLogin(){
return $this -> helper->getLoginUrl(Config::get('facebook.app_scope'));
}
public function login(){
return Redirect::to(self::getUrlLogin());
}
public function callback(){
dd(Input::all());
}
}
But get this error:
FatalErrorException in LoginFacebookController.php line 23:
Call to a member function getLoginUrl() on a non-object
Any solution?
Laravel 5 introduces the first-party Socialite package which includes support for Facebook. The documentation has more information on how to set it up but in the end, the code required to interact with Facebook is much easier:
$user = Socialize::with('facebook')->user();
Run these commands:
apt-get install php5-curl
composer update
Then, add the class in your providers and the Socialite alias in your aliases.

CodeIgniter Facebook SDK 4 with Composer

I'm trying to install and run the Facebook SDK on CodeIgniter using Composer.
CodeIgniter is installed and working nicely.
Composer support was added by doing the following:
curl -s http://getcomposer.org/installer | php
touch composer.json
Added the require lines to the composer.json file ("facebook/php-sdk-v4" : "4.0.*")
Ran composer update
All went to plan. Composer created a /vendor folder, and the Facebook SDK is there.
I then added Composer support to CodeIgniter by adding the line include_once './vendor/autoload.php'; to the top of index.php.
No errors at this point.
I'm now looking to call the SDK. I don't seem to be able to use any of the Facebook classes though. See below for things tried and failed...
var_dump(class_exists('Facebook')); shows bool(false)
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');
Spits out:
Fatal error: Class 'FacebookSession' not found in /var/sites/***/public_html/application/controllers/welcome.php on line 13
And a more full example:
<?php
class Welcome extends CI_Controller {
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
public function index()
{
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');
}
}
Spits out:
Fatal error: Welcome cannot use Facebook\FacebookSession - it is not a trait in /var/sites/***/public_html/application/controllers/welcome.php on line 5
You've mixed the position of the USE statement.
What you might do is to declare the classes from the FB SDK outside and before class, and not inside. By using Use inside a class you are pointing to trait functionality, which should be included into the class.
<?php
class MyClass extends MyBaseClass {
// this is a namespaced trait inside the class
// = extend class with trait
use SomeWhere\Trait;
}
?>
--
<?php
// this is the declaration of a namespaced class outside of the class
use SomeWhere\Class;
class MyClass extends MyBaseClass
{
public function helloWorld()
{
$c = new Class;
// ...
}
}
?>
--
Your code becomes:
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
class Welcome extends CI_Controller {
public function index()
{
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');
}
}

Categories