I check login at UserControler: Auth :: check () == true;
if (Auth::attempt(['email' => $request->email, 'password' => $request->password]))
{
var_dump(Auth::check());
}
but check login at Controller: Auth :: check () == false;
Please help me understand it. Below is my code:
<?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;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
function __construct()
{
$this->checkLogin();
}
function checkLogin()
{
var_dump(Session::get('user'));
view()->share('user_login', Auth::user());
}
}
Laravel 5.3 auth check in constructor returning false
you can't access the session or authenticated user in your
controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly
in your controller's constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4 or above:
Related
When I try to destroy a record with resource controller, laravel redirects to login page
although I applied auth middleware in the controller.
Laravel Version is 6.x.
Web.php
<?php
Auth::routes();
Route::resource('sliders', 'Admin\SliderController');
SliderController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Slider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Validator;
class SliderController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
...other methods
public function destroy(Slider $slider)
{
Slider::destroy($slider->id);
Session::flush('status', 'success');
return redirect('sliders');
}
}
}
I couldn't understand why it redirects to login page.
Help please
Thanks
Session flush is to delete all data https://laravel.com/docs/master/session#deleting-data
Use
$request->session()->flash('status', 'success');
I have a custom middleware where I want to use a singleton I use to pass php variables into my frontend, however I get the following error:
ReflectionException (-1)
Class App\Http\Middleware\Javascript does not exist
My middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Auth;
class AuthAdmin
{
public function handle($request, Closure $next)
{
$user = Auth::user();
if($user && $user['privileges'] > 2)
{
return $next($request);
}
app(Javascript::class)->put(['showLoginModal' => true]);
return redirect('/');
}
}
My ServiceProvider:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Helpers\Javascript;
class JavascriptServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(Javascript::class, Javascript::class);
}
}
composer dump-autoload didn't fix anything, I have been having problems where the Javascript Class is not found for some reason, any ideas or sugestions?
Try importing Javascript class in your middleware. It's trying to find it in wrong namespace.
middleware:
<?php
namespace App\Http\Middleware;
use App\Helpers\Javascript;
I am using laravel 5.2 and following is my code
I am getting error
ReflectionException in Route.php line 280:
Method App\Http\Controllers\Signup_controllers::guestcheckout() does not exist
whats wrong i am doing? plz help
this is my route.php
Route::group(array('prefix' => 'signup'), function()
{
Route::resource('/register', 'Signup_controllers#register');
Route::resource('/guestcheckout', 'Signup_controllers#guestcheckout');
Route::resource('/login', 'Signup_controllers#login');
Route::resource('/logout', 'Signup_controllers#logout');
Route::resource('/ajaxCheckCustomerEmailExist', 'Signup_controllers#ajaxCheckCustomerEmailExist');
});
this is my signup controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Session\Session1;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Image;
use Session;
use DB;
use Mail;
use App\Http\Models\Frm_mailing_list;
use App\Http\Models\Frm_contactus;
use App\Http\Models\Emailautoresponse;
use App\Http\Models\Adminemail;
use App\Http\Models\Emailsetting;
use App\Http\Models\Product_price;
class Signup_controllers extends Controller
{
public function index(Request $request)
{
}
public function register(Request $request)
{
include(public_path().'/app/Http/Controllers/action/register_controllers.php');
}
public function login(Request $request)
{
include(public_path().'/app/Http/Controllers/action/login_controllers.php');
}
public function logout()
{
Session::flush();
return Redirect::away(url('/login-registration'))->send();
}
public function guestcheckout(Request $request)
{
include(public_path().'/app/Http/Controllers/action/guestcheckout_controllers.php');
}
public function ajaxCheckCustomerEmailExist(Request $request)
{
//Checked By Ranjit
$email=$request->email;
$customerData=array('email'=>$email);
$Customer=new Customer;
$resultCustomer=$Customer->getByAttributesQuery($customerData);
if($resultCustomer['recordCount']>0){
echo "false";
}else{
echo "true";
}
}
}
when i try to call guestcheckout it says method not found even i have defined it
Route::get('/register', 'Signup_controllers#register');
You're including controllers within Controllers and alsorts of things are wrong with your code that are going to cause you problems
I'd consider reading the documentation to understand how Laravel works
You are using Resource Controllers incorrectly. See Laravel Documention.
https://laravel.com/docs/5.4/controllers#resource-controllers
change:
Route::resource('/guestcheckout', 'Signup_controllers#guestcheckout');
to
Route::post('/guestcheckout', 'Signup_controllers#guestcheckout');
and do the same thing for other routes, replace resource with post or get for your needs
Laravel resource routing assigns the typical "CRUD" routes to a
controller with a single line of code. and you call it like this :
Route::resource('photos', 'PhotoController');
So I am having a few dramas with my controllers. They seem to operate properly, however they don't seem to use __construct() at all in any of the controllers.. I'm trying to use this to update our users table to show last activity.
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use App\Helpers\UserHelper;
use App\Helpers\ForumHelper;
use App\Helpers\ShopHelper;
use Auth;
use Image;
use App\User;
use DB;
use Hash;
use File;
class AdminController extends BaseController
{
public function __construct() {
if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
$_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
if(Auth::check()) {
$time = strtotime("now");
DB::table('users')->where('userid', Auth::user()->userid)->update(['lastactivity' => $time]);
}
}
Any idea what I can do to check and try and get it working again?
I'm running Laravel Framework 5.4.28
A project I did similar on was running Laravel Framework version 5.2.45 and worked fine when I was doing that so I'm confused why this is happening on a newer version.
Any ideas how I can otherwise go about implementing the DB Update when loading stuff from my controllers?
If you want an activity tracker then add a web route middleware:
class ActivityTrackerMiddleware {
public function handle($request,$next) {
if ($request->user()) {
$request->user()->lastactivity = Carbon::now();
$request->user()->save();
}
return $next($request);
}
}
Add this in your web middleware:
'web' => [ ...
StartSession::class,
ActivityTrackerMiddleware::class,
...
];
You can also limit it to the admin controller if you prefer.
Hello i am trying to use a trait from controller in my register controller but it can't seem to find it
the error message:
Trait 'MailVerification' not found
The class in which i want to use the trait
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
use MailVerification;
Here i call the function
protected function create(array $data)
{
$mail = $data['email'];
$this->sendVerification($mail);
Here is the trait in the class i am trying to import it from
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
trait MailVerification
{
public function sendVerification($mail)
{
$verification_code = str_random(30);
Mail::send('mail.verify', ['verification_code' => $verification_code, 'mail' => $mail], function ($message) use ($mail)
{
$message->from('test#laravel.com');
$message->to($mail);
});
Session::flash('message', "Please check you're email to verify your account");
return redirect('/');
}
}
class MailController extends Controller
{
I have the trait outside of my class i don't know if this is correct but it was giving me an error while it was inside the class.
The namespace of your controller RegisterController and your trait MailVerification is different...
So, you'll have to add this line to your RegisterController
use App\Http\Controllers\MailVerification;
Also, I suggest you to put all your traits inside App\Traits folder instead of your controller. Try following a simpler way if possible :)
Edit --
This is how you register controller should look like
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MailVerification;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers, MailVerification;
//Your code here....
}