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.
Related
I am trying to extend the package controller in my base laravel controller. Tried importing class using below code which shows error as class not found.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ProductController as ControllersProductController;
use App\Imports\ProductsImport;
use AvoRed\Framework\AvoRedProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Avored\Framework\Catalog\Controllers\ProductController;
class ProductControllers extends Controller
{
private $avored_product;
public function __construct(ProductController $p) {
$this->avored_product = $p;
}
public function index() {
echo $this->avored_product;
}
Tried multiple options by researching on it couldn't find the same. Request all to please guide me with same.
Could you share with us the exact error message? From your code snippet I can't see which class couldn't be found.
And which Avored package are you referring to? I guess avored-laravel-ecommerce?
If you want to extend the ProductController-from the package, you have properly extend from that controller.
<?php
namespace App\Http\Controllers\Admin;
use Avored\Framework\Catalog\Controllers\ProductController as AvoredProductController;
class ProductControllers extends AvoredProductController
{
public function index() {
// Do your thing in here
}
}
You can now override the controller methods to your liking.
I refer to Laravel5.2 LINE Login and try to login my website by Line.
But have some error in my project:
"Driver [line] not supported."
full version CODE
CODEļ¼
1.SocialController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Socialite\Contracts\Factory as Socialite;
class SocialController extends Controller
{
protected $socialite;
public function __construct(Socialite $socialite)
{
$this->socialite = $socialite;
}
function redirect(){
return $this->socialite->driver('line')->redirect();
}
function callback(){
$user = $this->socialite->driver('line')->user();
var_dump($user);
}
}
Laravel Socialite doesn't support line driver.
If you want that, I think you have three options:
You can do with any HTTP Client (something like guzzle or curl)
You can use PHP SDK (something like line-bot-sdk-php)
You may find some packages (something like socialiteproviders/line)
*** Edited
I'm recently created laravel line package. You can check
https://github.com/naingminkhant/laravel-line
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(....)
Trying to upgrade to Laravel and following this Laravel 5 upgrade
But when it comes to Filter routes for Sentry. I get this error:
FatalErrorException in RouteServiceProvider.php line 38: Class 'App\Providers\Session' not found
Related to this copied and pasted from previous L4 filter:
namespace App\Providers;
use Cartalyst\Sentry\Facades\Laravel\Sentry;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider {
protected $namespace = NULL; //using composer
public function boot(Router $router)
{
parent::boot($router);
Route::filter('Sentry', function(){
if (!Sentry::check()) {
Session::put('loginRedirect', Request::url());
return Redirect::guest('login');
}
});
}
}
The 'Session' is the issue.
Any help appreciated, thanks.
You have to import Session.
use Session;
Or prepend it with a backslash
\Session::put('loginRedirect', Request::url());
The same goes for Request and Redirect
Session facade exists under global namespace. Since your file is under App\Providers namespace, you have to use \Session::put('loginRedirect', Request::url()); instead of Session::put('loginRedirect', Request::url());.
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');
}
}