I'm new to Laravel and Spark trying to figure out how/where I should add my new Controllers and add Socialite.
In my providers I added
Laravel\Socialite\SocialiteServiceProvider::class,
In my aliases I added
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Here's what my app/HTTP/Controllers/Auth/LoginController class looks like
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the GitHub authentication page.
*
* #return Response
*/
public function redirectToProvider()
{
return Socialite::with('github')->redirect();
}
/**
* Obtain the user information from GitHub.
*
* #return Response
*/
public function handleProviderCallback()
{
$user = Socialite::driver('github')->user();
// $user->token;
}
}
I get undefined class Socialite when I add use Socialite;
I tried composer dump-autoload and php artisan config:clear but nothing is working. Am I doing something wrong?
I'm using Laravel 5.4 and Socialite 3.0
Thanks!
php artisan clear-compiled
composer dump-autoload
php artisan optimize
This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again.
Related
I'm trying to implement the reset password function using the built-in function from Laravel 5.7 as i have defined my routes in my web.php. I tried running php artisan route:list , It gave me an exception
UPDATE
Sorry for the lack of information given. I have already ran the command php artisan make:auth previously and the Auth::routes() has already been defined in web.php I am trying to access function resets in ResetPasswords traits through my ResetPasswordControllerbut it gave an exception
Class App\Http\Controllers\ResetPasswordController does not exist
I am using the pre-defined controller that is located at App\Http\Controllers\Auth\ResetPasswor.php
ResetPasswordController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
public function reset(Request $request){
$reset = $this->reset($request);
}
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
web.php
Auth::routes();
Route::post('password/reset','ResetPasswordController#reset');
SOLUTION
I have figured out where did i do wrong i had to add a Auth\ in my routes
Route::post('password/reset','Auth\ResetPasswordController#reset');
I am trying to run test page using Laravel.
When I use Controller, every time I got message:
Laravel: ReflectionException - Class App\Http\Controllers\XXXX does not exist
Does somebody knows where problem is?
This is my routes/web.php:
Route::get('/hi', 'HiController#index');
HiController.php (it is in correct folder structure: app/Http/Controllers/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HiController extends Controller
{
public function index(){
return "test";
}
}
RouteServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* #return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* #return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Also, when I use this code in routes/web.php it works:
Route::get('/hi', function (){
return "hi";
});
It's clean Laravel 5.6 installation, on Windows, wamp64.
I tried also with "composer dump auto-load" and "php artisan config:clear" but nothing works.
Thank you in advance.
Run the following (assuming your app name is app):
php artisan app:name app
Then use the following namespace also in your controller:
app\Http\Controllers\Controller
Q: why XXXX if it's HiController? About the issue, if it returns the hi, technically, it has to work if it's vanilla.
Since its on windows and wamp64, I don't think file permissions are a thing so let's skip that.
Make sure the name of the file is indeed correct
Make sure the file indeed exists in the correct directory
Make sure the namespce is namespace App\Http\Controllers;
Add this use, just in case: use App\Http\Controllers\Controller;
Just to be sure, make sure in your composer.json you've got this (you should have since it's default):
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
If none work, try creating a new controller php artisan make:controller SomeController and try it with this one.
tried every option, the only that worked for me was restarting the vm, ngingx or laravel had something cached or something similar that throw this error.
I just installed Laravel under xampp in a project with name blog1. The documentation says that default namespace is App.
I create the following controller :
namespace App\Http\Controllers;
use App\User;
use App\Repositories\UserRepository;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* The user repository implementation.
*
* #var UserRepository
*/
protected $users;
/**
* Create a new controller instance.
*
* #param UserRepository $users
* #return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$user = $this->users->find($id);
return view('user.profile', ['user' => $user]);
}
}
with the name testController.php under public directory.
when I http://localhost/blog1/testController.php
I get the following Class 'App\Http\Controllers\Controller' not found in C:\xampp\htdocs\blog1\public\testController.php on line 10.
Any suggestion?
I tried to set the namespace to blog1 but now nothing happened with blog1/Http?Controller.
I reinstall Laravel through composer.
Read about PSR-4, PSR-0 (deprecated) and autoloading in PHP. Error is thrown because class name (UserController) must be exact to the filename testController.php so you should name your file UserController.php or your class testController.
Also read about Laravel's routing and composer's autoloading.
The error from laravel is valid.
You're placing your file TestController.php under public directory, and giving the class a namespace of - App\Http\Controllers.
You should place your TestController.php inside the directory:
app > Http > Controllers
By this, your code will not throw an error again!
See more about Laravel Controllers & Namespacing & Laravel Directory Structure
Hope this helps!
I am using Laravel 5.3 and trying to implement authentication system. I used php artisan command make:auth to setup it. I edited the views according to my layout and redirecting it my dashboard page instead of home (set as default in setup). Now, when I am trying to logout it throwing me this error
NotFoundHttpException in RouteCollection.php line 161
My code in routes/web.php is:
Auth::routes();
Route::get('/pages/superadmin/dashboard', 'HomeController#index');
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('dashboard');
}
}
Auth/Login Controller.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
I tried the solutions on this page: How to set laravel 5.3 logout redirect path? but it didn't work and showing these errors:
ReflectionException in Route.php line 339:
Class App\Http\Controllers\Auth\Request does not exist
I want to redirect it to login page which is in auth/ folder.
I finally solved this issue by adding this line in my LoginController.php
protected $redirectAfterLogout = 'auth/login';
and editing this file \vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
It will use the default '/', if you don't provide $redirectAfterLogout in this file. You can also find it on github. Link is at the end of the answer.
public function logout()
{
return redirect(property_exists($this, 'redirectAfterLogout') ? $this- >redirectAfterLogout : '/');
}
You can also check it here: https://github.com/laravel/framework/commit/aa1204448a0d89e2846cbc383ce487df6efd9fc8#diff-b72935cc9bfd1d3e8139fd163ae00bf5
Hope it helps someone.
Thank You
If you want to continue to use GET to logout
Route::get('logout', 'Auth\LoginController#logout');
or
Route::get('logout', '\App\Http\Controllers\Auth\LoginController#logout');
Tested in Laravel 5.4
The solution that I believe works the best is overriding the inherited "logout" method that gets called from inside the app/Http/Controllers/Auth/LoginController.php file. Do this by adding the following method to this class:
/**
* Log the user out of the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect('/login');
}
As the "LoginController" class inherits from Illuminate\Foundation\Auth\AuthenticatesUsers, you should safely be able to override this method (in the LoginController) WITHOUT editing the actual vendor file itself... Editing the AuthenticatesUsers file, or any vendor file would cause major headaches down the road if you ever wanted to upgrade...
The only additional step here is that you need to include the following line at the top of the LoginController class:
use Illuminate\Http\Request;
In Laravel 5.3 logout is http post instead of http get. You can logout via post request like Taylor Otwell do in auth scaffolding.
<a href="{{ url('/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Redirecting logout action to login page means to me there is no page in the site available unless user is authenticated.
I am not a big fan of touching the vendor directory as it is suggested above. It is true, Laravel 5.4 vendor/laravel/framework/src/Illuminate/Foundation/Auth/AutenticateUser->logout() redirects to '/'. There is no parameter available to change it. So let's keep it that way and just add an 'auth protection' to the route '/' (home)
in /routes/web.php add
Route::get('/', function () { return view('home'); })->middleware('auth');
Isn't it the simplest way ?
Works on laravel 5.6
File => app/Http/Controllers/Auth/LoginController.php
use Illuminate\Http\Request;
class LoginController extends Controller
{
... ... ...
... ... ...
... ... ...
/**
* Log the user out of the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect('/login');
}
}
Thanks The Virtual Machinist for your answer.
I build basic Auth in laravel 5.3 user make:auth. In laravel 5.3 they separate login and register controller in Auth\LoginController and Auth\RegisterController.
Below is my Auth\LoginController
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
The problem is when I'm try to edit AuthenticatesUsers in Illuminate\Foundation\Auth\AuthenticatesUsers, that not affected at all. I even try to rename the class/trait name on Illuminate\Foundation\Auth\AuthenticatesUsers, but the script still works.
So where is the actual AuthenticatesUsers? Because my sublime can only find one file with that name.
Thank You
There is only one AuthenticatesUsers, and it will be in vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php. If you're editing it and it's not having any effect then you could be editing the file in a different project.
It's worth noting that you should never edit this file anyway. Everything in your vendor/ folder should be left as-is, as when Composer runs it will replace any changes you've made. If you want to make changes you should extend or override the methods you need to.