Can't use laravel singleton inside Middleware - php

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;

Related

inheritance i laravel :Class name must be a valid object or a string

this is my parent class which is a user class that has the main crud operations
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\UserRepository; //<----------- Here
class UserController extends Controller
{
protected $model;
public function index()
{
$users = $this->model::all();
return view('users.index', compact('users'));
}
}
this is my child class which is one of my user roles , it have the same crud operation but it need some more functinality
<?php
namespace App\Http\Controllers;
use App\Models\Teacher;
use App\Http\Controllers\UserController;
class TeacherController extends UserController
{
public function __construct()
{
$this->model = Teacher::class;
}
}
when I try to access the route i get this error : Class name must be a valid object or a string
at :
$users = $this->model::all();
Well, it seems my Laravel project used old cached routes. Just run
php artisan route:clear
from time to time before debugging anything.

undefined type Auth

namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(Auth::Attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Change the Auth namespace to:
use Illuminate\Support\Facades\Auth;
You may use the auth helper instead and then no worries about any class name
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(auth()->attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Try this :
\Auth::attempt([...]);
I think this is helpfull..
Use the Auth namespace to:
use Illuminate\Support\Facades\Auth;

Lavarel Auth::check() not woking

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:

Calling model inside a middleware laravel

I am trying to call a model Readdb inside a middleware. but I am getting error :
Class 'app\Models\Readdb' not found
My middlware code is:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Models\Readdb;
class Adminlogin {
public function handle($request, Closure $next) {
if (!$request->session()->has('userid')) {
$db = new Readdb();
return response()->view('admin.auth.login');
} else {
return response()->view('admin.dash');
}
return $next($request);
}
}
Readdb File:
<?php
namespace App;
use \Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Readdb extends Model {
public function get_setting($name) {
$data = DB::table('setting')->select('value')->where('name', '=', $name)->get();
return $data->value;
}
}
path to readdb is: /var/www/html/ecommerce/app/Models/Readdb.php
Your Readdb exists in the namespace App, not App\Models, so either do this in the middleware:
use App\Readdb;
Or, if your composer.json says to look for App\Models in the app/Models dir, update the namespace on Readdb:
namespace App\Models;

Laravel 5.3 Trait not found

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....
}

Categories