Laravel: Auth error after upgrade 5.1 -> 9 - php

I've been handed an old Laravel application, which was running v5.1 and have used Shift to upgrade it through the Laravel v9.
The whole app is behind a login ~ and on submit of the login form (correct or fail), it returns:
Too few arguments to function App\Providers\AppServiceProvider::App\Providers\{closure}(), 1 passed in /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php on line 421 and exactly 3 expected
and the trace ends in app  / Providers / AppServiceProvider.php: 37, line 37 being function ($sql, $bindings, $time)
....
error_reporting(0);
// public function boot(Guard $auth) {
view()->composer('*', function ($view) use ($auth) {
if ($auth->user()) {
// get the current user
$currentUser = $auth->user();
Log::info('Executed by '.$currentUser->email);
// pass the data to the view
$view->with('currentUser', $currentUser);
}
});
DB::listen(
function ($sql, $bindings, $time) {
$a = print_r($sql, 1);
$b = print_r($bindings, 1);
$logmessage = $a;
if (! empty($b)) {
$logmessage .= ' : '.$b;
}
Log::info($logmessage);
}
);
...
During the Shift process, re. the login/auth process it noted the following:
Laravel 5.3 split the Auth/AuthController.php into the Auth/LoginController.php and the Auth/RegisterController.php. Since yours appears to be customized, Shift did not remove it. You should compare your Auth/AuthController.php with the newly added controllers and merge your changes accordingly.
and
If you are using Authentication, you should upgrade any routes using AuthController and PasswordController to use the new authentication controllers. Laravel recommends using the Auth::routes() which registers the proper routes for the new authentication controllers. This method also registers a POST route for /logout instead of a GET route.
but the highlighted error line seems related to the database connection...
A quick sanity check of database connection:
try {
\DB::connection()->getPDO();
echo \DB::connection()->getDatabaseName();
} catch (\Exception $e) {
echo 'None';
}
returns the correct database connection, as expected.
I'm not a Laravel-specific dev so trying to muddle my way through and no doubt need to provide more debugging info here, so just let me know what else would be helpful.
Thanks in advance

Related

Design pattern that handles multiple steps

So I have a complicated onboarding process that does several steps. I created a class that handles the process but I've added a few more steps and I'd like to refactor this into something a bit more manageable. I refactored to use Laravel's pipeline, but feel this may not be the best refactor due to the output needing to be modified before each step.
Here is an example before and after with some pseudo code.
before
class OnboardingClass {
public $user;
public $conversation;
public function create($firstName, $lastName, $email){
// Step 1
$user = User::create();
// Step 2
$conversation = Conversation::create(); // store information for new user + existing user
// Step 3
$conversation->messages()->create(); // store a message on the conversation
// Step 4
// Send api request to analytics
// Step 5
// Send api request to other service
return $this;
}
}
after
class OnboardingClass{
public $user;
public $conversation;
public function create($firstName, $lastName, $email){
$data = ['first_name' => $firstName, ...]; // form data
$pipeline = app(Pipeline::Class);
$pipeline->send($data)
->through([
CreateUser::class,
CreateNewUserConversation::class,
AddWelcomeMessageToConversation::class,
...
])->then(function($data){
// set all properties returned from last class in pipeline.
$this->user = $data['user'];
$this->conversation = $data['conversation'];
});
return $this;
}
}
Now within each class I modify the previous data and output a modified version something like this
class CreateUser implements Pipe {
public function handle($data, Closure $next) {
// do some stuff
$user = User::create():
return $next([
'user' => $user,
'other' => 'something else'
]);
}
}
In my controller I am simply calling the create method.
class someController() {
public function store($request){
$onboarding = app(OnboardingClass::class);
$onboarding->create('John', 'Doe', 'john#example.com');
}
}
So the first pipe receives the raw form fields and outputs what the second pipe needs to get the job done in its class, then the next class outputs the data required by the next class, so on and so forth. The data that comes into each pipe is not the same each time and you cannot modify the order.
Feels a bit weird and I'm sure there is a cleaner way to handle this.
Any design pattern I can utilize to clean this up a bit?
I think you could try using Laravel Service Provider, for example, you could build a login service provider; or Event & Listener, for example, you could build an listener for login and triggers a event to handle all the necessary logics. Can't really tell which one is the best since outcome is the same and it makes same amount of network requests, but it's more on personal preferences

get loggedIn user in View Composer not working(Sentinel - Laravel 5.4)

I am using Sentinel in Laravel 5.4. What I am trying to do is: get logged user detail but Sentinel::getUser() returns null. For this process, I have seen this instruction in this answer
. I am following using View Composer method.
Steps I have done
I have created a file ViewComposerServiceProvider inside Providers folder. It looks like:
public function boot()
{
$user = Sentinel::getUser(); //<<-- main error: dd($user) returns empty
$userDetail = UsersDetail::where('user_id', $user->id)->firstOrFail();
if ( is_null ($userDetail) ) {
$userDetail = new UsersDetail;
}
view()->composer('backend.*', function($view) {
$view->with('userDetail', $userDetail);
//$view->with('userDetail', 'Test'); //this works fine
});
}
Then, I register this provider in config/app.php Providers array as
App\Providers\ViewComposerServiceProvider::class,
When, I pass other variables in userDetail, it's working perfectly. But, I cannot get the logged in user detail. Am I missing something?
Following the first solution from this answer also seems not working since, construct are run prior to the Middleware. Any help please.
Go to app\Providers\AppServiceProvider.php
Then your serviceProvider.php boot method like below
public function boot()
{
$user = Sentinel::getUser(); //<<-- main error: dd($user) returns empty
$userDetail = UsersDetail::where('user_id', $user->id)->firstOrFail();
if ( is_null ($userDetail) ) {
$userDetail = new UsersDetail;
}
View::composer('userDetail', function($view) use($userDetail ) {
$view->with('userDetail ',$userDetail );
});
}
Then your userDetail.blade.php you can access userDetail data like this
{{ $userDetail }}

Laravel 5.4: Custom view for route-model-binding not finding the ID

As I'm beggining with Laravel, this should be a simple one:
How can I define a custom view to be rendered when my route model binding just can't find the given ID?
Here's my route:
Route::get('/empresa/edit/{empresa}', 'EmpresaController#edit');
Here's my controller's method:
public function edit(Empresa $empresa)
{
if ((!isset($empresa)) || ($empresa == null)):
//I get that this won't work...
return 'Empresa não encontrada';
endif;
return view('Empresa.dadosEmpresa')->with('empresa', $empresa)->with('action', URL::route('empresa_update', ['id', $empresa->id]))->with('method', 'PATCH');
}
Here's my "attempt" to use an error handler:
public function render($request, Exception $exception)
{
if ($e instanceof ModelNotFoundException)
{
//this is just giving me a completely blank response page
return 'Empresa não encontrada';
}
return parent::render($request, $exception);
}
How is this really done?
1. The formal way (but would it be really needed to customize in this way?)
First of all, what Laravel does is, if there is not Model Row in DB with the given id, it sends 404 response, automatically.
If a matching model instance is not found in the database, a 404 HTTP response will be automatically generated.
So if you wanna show your customized view, you need to customize error handling.
So in RouteServiceProvider file, make sure it throws custom exception using 3rd param like follwoing:
public function boot()
{
parent::boot();
Route::model('empresa', App\Empresa::class, function () {
throw new NotFoundEmpresaModelException;
});
}
And then do same thing in the render function as you tried before.
2. The casual way - Pretty easy to go
I d rather suggest that you do not use model injection ability, but handle the request yourself.
So take the empresa id value as it is, and then try to find the right data, and if not found, then make your custom logic. That should be pretty easy to go.
public function edit(Request $request, $empresa)
{
$empresaObj = Empresa::find($empresa);
if (!$empresa) {
return 'Empresa não encontrada';
}
return view('Empresa.dadosEmpresa')->with('empresa', $empresa)->with('action', URL::route('empresa_update', ['id', $empresa->id]))->with('method', 'PATCH');
}

Set current route programmatically; setCurrentRoute is missing

I just upgraded to Laravel 4.1 and can no longer use a function I was using in the past. I wrote a function to redirect an incoming request to another route, get the result, and replace the current route with the original incoming route. I used this on my frontend controllers to consume my own API which is defined in the same application.
Here is the function:
public static function redirectRequest($newRoute, $verb, $args = null)
{
// store the original request data and route
$originalInput = Request::input();
$originalRoute = Route::current();
$request = $args === null ? Request::create($newRoute, $verb) : Request::create($newRoute, $verb, $args);
// replace the request input for the new route...
Request::replace($request->input());
try
{
$response = Route::dispatch($request);
return $response;
}
catch (\Exception $e)
{
throw $e;
}
finally
{
// replace the request input and route back to the original state
Request::replace($originalInput);
Route::setCurrentRoute($originalRoute);
}
}
And I would use it like:
Helpers::redirectRequest('/api/v1/someroute', 'GET');
The problem is that, when I try to return things to the way they were before the redirect, I can't. setCurrentRoute has been removed from 4.1 and I can't figure out how to reset the current route.
One thing that I have done in the past is used the actual routes.php to handle that. If you register a "catchall" route as:
Route::get('api/v1', 'ApiController#route');
As long as this is after all other routes (Laravel uses the first matching route) you can then handle that within your ApiController as
public function route($uri) {
// Handle your API route using the $uri variable
}
This may not be the solution that you are looking for, but I have found it very convenient.

access to extended class in symfony 1.4

so... I basically follow the practical symfony book, and encountered following problem.
I have properly (i guess) installed sfGuardPlugin, built the models, sqls etc, created user and tried to log in with the username and password entered.
i got the following error message:
Fatal error: Call to undefined method sfGuardUserPeer::retrieveByUsername() in /***/plugins/sfGuardPlugin/lib/validator/sfGuardValidatorUser.class.php on line 53
it looks quite weird to me, because the problematic part of sfGuardValidatorUser class looks like this:
// user exists?
if ($user = sfGuardUserPeer::retrieveByUsername($username))
{
// password is ok?
if ($user->getIsActive() && $user->checkPassword($password))
{
return array_merge($values, array('user' => $user));
}
}
while sfGuardUserPeer has just the empty class:
class sfGuardUserPeer extends PluginsfGuardUserPeer
{
}
that extends PluginsfGuardUserPeer, so i checked it out too:
class PluginsfGuardUserPeer extends BasesfGuardUserPeer
{
public static function retrieveByUsername($username, $isActive = true)
{
$c = new Criteria();
$c->add(self::USERNAME, $username);
$c->add(self::IS_ACTIVE, $isActive);
return self::doSelectOne($c);
}
}
that's the missing function!
so - what is wrong? why doesn't it work?
i have already tried all the solutions found with google, but none of them work :/
finally found it!
the
symfony propel:build-model
task unnecessarily generated the sfGuard classes in the model directory from the schema file located in the plugin directory, while all the classes were already present in the sfGuard folder.
geez, that shouldn't happen in such a well-developed framework and plugin...
Simply put that
public static function retrieveByUsername($username, $isActive = true)
{
$c = new Criteria();
$c->add(self::USERNAME, $username);
$c->add(self::IS_ACTIVE, $isActive);
return self::doSelectOne($c);
}
Code into your sfGuardUserPeer class, this will sort out the issue, I did the same when I got this error, it worked for me..

Categories