BindingResolutionsException Target[Laravel\Socialite\Contracts\Factory] is not instantiable - laravel

i am trying to use socialite for make "Login with facebook" but i am getting this error continuously BindingResolutionsException Target[Laravel\Socialite\Contracts\Factory] is not instantiable. Please help me.
'providers' => [
....
Sun\Flash\FlashServiceProvider::class,
Laravel\Socialite\SocialiteServiceProvider::class
],
'aliases' => [
....
'Flash' => Sun\Flash\FlashFacade::class,
'PmhAuth' => app\Library\Auth\PmhAuth\PmhAuthFacades::class,
'Socialite' => Laravel\Socialite\Facades\Socialite::class
],
here is my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
class SocialAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
public function callback()
{
}
}

Since you've aliased Socialite in your config:
'Socialite' => Laravel\Socialite\Facades\Socialite::class
Try importing the alias:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Socialite;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class SocialAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
public function callback()
{
}
}

Related

Laravel :Target class [LoginController] does not exist."

Even the controller is in the right folder an has the right name, when i test it in thunder client i get:
{ "error": "Error en la aplicación
(Illuminate\Contracts\Container\BindingResolutionException):Target
class [LoginController] does not exist." }
Here's my controller in app/http/Controllers/api
<?php
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use App\Models\Usuario;
class LoginController extends Controller
{
public function login(Request $request)
{
$usuario = Usuario::where('login', $request->login)->first();
if (
!$usuario ||
!Hash::check($request->password, $usuario->password)
) {
return response()->json(
['error' => 'Credenciales no válidas'],
401
);
} else {
$usuario->api_token = Str::random(60);
$usuario->save();
return response()->json(['token' => $usuario->api_token]);
}
}
}
and here's my route in the folder routes/api
Route::post('login', [LoginController::class, 'login']);
i have tried changing the name, i changed the route to
Route::post('login', [\App\Http\Controllers\Api\LoginController::class, 'login']);
and then said
{ "error": "Error en la aplicación
(Symfony\Component\ErrorHandler\Error\FatalError):Cannot declare
class LoginController, because the name is already in use" }
You are missing your namespace :
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
[...]

Class "App\Http\Controllers\Auth\Mail" not found

How can I resolve this error? I am trying to customize the default email template on laravel. This is the code for the controller that sends the email.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use App\Models\User;
use Illumunate\Auth;
class EmailVerificationNotificationController extends Controller
{
public function store(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);
}
Mail::send('email.template', $request->user(), function($mail) use($data){
$mail->to($request->user()->email, 'no-reply')->subject("Verify Email Address");
$mail->from('admin#raketlist.com','testing');
});
$request->user()->sendEmailVerificationNotification();
return back()->with('status', 'verification-link-sent');
}
}
Add use Illuminate\Support\Facades\Mail; to other uses.

Laravel 5.4 - Validation not working

Am I doing something wrong with the validation
<?php
namespace App\Http\Controllers\Auth;
use Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class SignupController extends Controller
{
public function postSignup(Request $request)
{
if($this->validate($request, [
'first_name' => 'required|max:255'
])){
echo json_encode(array('TRUE'));
}else{
echo json_encode(array('FALSE'));
}
}
}
The request data is right...
But the validator always return null... and the return json is the FALSE
validate does not return true or false. It throws an exception when it fails and returns nothing if it succeeds.
thank you #apokryfos

Returning views using controller present somewhere else other than Views folder

How to return view present somewhere other than views folder from the controller in Laravel? I am making a project divided into modules. I want to return the view of the module from the controller of the main project. How to do it?
Because currently when I try to route to other links it shows:
MethodNotAllowedHttpException in compiled.php line 8895
Routes/web.php:
Route::get('/', function () {
return view('welcome');
});
Route::post('/navpage1',[
'uses'=>'ProjectController#nextpage1',
'as'=>'navpage1'
]);
Route::post('/navpage2',[
'uses'=>'ProjectController#nextpage2',
'as'=>'navpage2'
]);
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class ProjectController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function nextpage1()
{
return view('C:/xampp/htdocs/larve/app\Modules\Course_Entry\views\welcome');
}
public function nextpage2()
{
return view('C:/xampp/htdocs/larve/app\Modules\Log_in_blog_post\views\welcome');
}
}
App/Modules/ServiceProvider.php:
<?php namespace App\Modules;
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
$modules = config("module.modules");
while (list(,$module) = each($modules)) {
if(file_exists(__DIR__.'/'.$module.'/web.php')) {
include __DIR__.'/'.$module.'/web.php';
}
if(is_dir(__DIR__.'/'.$module.'/Views')) {
$this->loadViewsFrom(__DIR__.'/'.$module.'/Views', $module);
}
}
}
public function register(){}
}
I know something is wrong with my controller, but i am just trying to figure out how to do it...
I have given more code here: Laravel program divided into modules
change the paths in app->config->view.php.
'paths' => [
realpath(base_path('app\Modules\Log_in_blog_post\views')),
],
then use in controller:- view('welcome').

Serious issues understanding namespacing in Laravel 5

I am having problems understanding namespacing. It just feels so stupid that for every Facade or helper I want to include, I need to included it in the controller file.
So, say I have this exerpt of my controller:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Auction;
use App\Bid;
use App\BidBuy;
use App\Http;
use Redirect;
use Session;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
class PayPalController extends Controller {
private $data;
public function getPayment()
{
redirectIfNotLoggedIn();
}
The line with redirectIfNotLoggedIn(); is a function I want to call that is stored in App/Http/helpers.php
This is the file: helpers.php
<?php namespace App\Http;
class HelperFunctions {
// Filter for guest only pages
public function redirectIfLoggedIn() {
if (Session::has('steamid')) {
return Redirect::to('/');
} else {
return false;
}
}
// Filter for logged users only pages
public function redirectIfNotLoggedIn() {
if (Session::has('steamid')) {
return true;
} else {
return Redirect::to('/');
}
}
}
Back in the controller I have included: use App\Http;
When I go to call that function I get this:
FatalErrorException in PayPalController.php line 20:
Call to undefined function App\Http\Controllers\redirectIfNotLoggedIn()
EDIT:
Now I have: HelperFunctions.php located at App/Helpers/HelperFunctions.php:
<?php namespace App\Helpers;
use Session;
use Redirect;
// Filter for guest only pages
function redirectIfLoggedIn() {
if (Session::has('steamid')) {
return Redirect::to('/');
} else {
return false;
}
}
// Filter for logged users only pages
function redirectIfNotLoggedIn() {
if (Session::has('steamid')) {
return true;
} else {
return Redirect::to('/');
}
}
Excerpt of composer.json:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"psr-0": {
"Ignited\\LaravelOmnipay": "src/"
},
"files": [
"app/Helpers/HelperFunctions.php"
]
},
And this is my controller:
<?php namespace App\Http\Controllers;
//use App\Helpers\HelperFunctions;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Auction;
use App\Bid;
use App\BidBuy;
use Redirect;
use Session;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
class PayPalController extends Controller {
private $data;
public function getPayment(\App\Helpers\HelperFunctions $helper)
{
$helper->redirectIfNotLoggedIn();
}
.. rest of the controller code here ..
}
Do not use helpers for this. You are violating pretty much everything related to OOP in doing so.
Instead, use Middleware.
In your controller, simply require the auth middleware:
public function __construct()
{
$this->middleware("auth");
}
You are trying to solve a solved problem. If you need to do something before executing a route/controller method, middleware (the Laravel 5 replacement for route filters) should be used.
You will likely have to customise your own middleware, however, then add it to the middleware array in app/Http/Kernel.php. Just use php artisan make:middleware CustomAuthMiddleware.
You could use your HelperFunctions via method injection in your controller. Like this:
public function getPayment(\App\Http\HelperFunctions $helper)
{
$helper->redirectIfNotLoggedIn();
}
It's not tested.

Resources