furture more it says.
in Controller.php line 82.
at Controller->__call('validate', array(object(Request), array('email' => 'required|string', 'password' => 'required|string')))
in AuthenticatesUsers.php line 63
note:
every thing was working fine till yesterday morning.my laravel version is 5.4.36.
Put this line in your class
use ValidatesRequests;
Right below
class classname ...... {
use ValidatesRequests;
}
Your LoginController doesn't extend from the base Controller, App\Http\Controllers\Controller or this base class is missing a trait. This class was setup to use the Illuminate\Foundation\Validation\ValidatesRequests trait, which gives you the validate method.
If your base Controller does not use this trait, then you can add the use statement to use this trait so that you will have the validate method in all your Controllers.
Related
I'm stuck on a weird issue...
I have a simple middleware.
$request->merge([
'user' => $jwt->toArray(),
'app_version' => $request->header('app-version')
]);
When I do a dump right before the $next($request); the it has the user object and the app_version. All good! See image below:
Moving to the controller. When in the function of defined in the route, and doing a dump($request); it has the user Object
The InstallController extends the controller.php which extends the use Illuminate\Routing\Controller as BaseController;
In the controller.php we have a construct function.
For some reason here the $request is empty and lost the object.
Why is this happening? And why is this available in the InstallController but not in the parent?
What turned out is the following. In Laravel lumen, controller constructors are called after the middleware has completed the request. In Laravel the constructors are called before the middleware.
Meaning that adding params to the request in the middleware in Laravel doesn't work.
Thanks for pointing me in the right direction.
I am new to laravel, i am trying to store a tweet to database and i need to insert user id, but this gives me an error
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tweet;
class TweetController extends Controller
{
public function store(Request $request){
return Tweet::create([ 'tweet' => request('tweet'), 'user_id' => Auth::id()]);
}
}
error i recive:
App\Http\Controllers\Auth' not found in file
/Applications/AMPPS/www/Twitter/Twitter/app/Http/Controllers/TweetController.php
Any idea?
Yes, you are missing the import, that's why it tries to find it in the Controller location, so put
use Illuminate\Support\Facades\Auth;
// or
use Auth; // you must have the Auth alias in the config/app.php array
as an import, or use the helper function auth()->id() instead.
So instead of mass-assigning the user, you can do the following, in your User model add this:
public function tweets()
{
return $this->hasMany(Tweet::class);
}
Then in your controller just do this:
auth()->user()->tweets()->create([ 'tweet' => request('tweet') ]);
You can use auth() helper to get user id:
auth()->user()->id
I have created a custom authentication and everything is working fine.
Now I am trying to add the Throttlelogins to prevent multiple incorrect login attempts. But The ThrottleLogins doesn't seem to load.
Q: What am I missing here? or am I doing something wrong?
The exception:
Method
App\Http\Controllers\Auth\CustomersLoginController::hasTooManyLoginAttempts
does not exist.
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Auth;
class CustomersLoginController extends Controller
{
public function __construct()
{
$this->middleware('guest:customers');
}
public function ShowLoginForm()
{
return view('auth.customer-login');
}
public function login(Request $request)
{
$v = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if(Auth::guard('customers')->attempt(['email'=>$request->email,'password'=>$request->password],$request->remember)){
return redirect()->intended(route('customerdashboard'));
};
return $this->sendFailedLoginResponse($request);
}
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
]);
}
public function username()
{
return 'email';
}
}
Error Message
Can someone please explain what am I mssing?
The error says you are missing a function: hasTooManyLoginAttempts
In the function login you can see it's trying to call the function but it does not exist in your class. This is where it goes wrong.
update
In the AuthenticateUsers class, which you tried to copy, it's using ThrottlesLogins trait, which you are missing in your controller.
Update your controller like so:
class CustomersLoginController extends Controller
{
use ThrottlesLogins;
Another update
You tried to import the Trait which Laravel uses in their own Login. However this will not work here's why:
When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.
namespace App\Http\Controllers\Auth;
So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:
use Illuminate\Foundation\Auth\ThrottlesLogins;
Now that you have imported the ThrottlesLogins, which is actually a trait, now inside the class you use it to expose all of the methods inside.
I created a fresh Laravel framework.
I created a controller named PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Http\Controllers\Controller;
class PostsController extends Controller
{
public function index()
{
$posts = Post::get();
return response()->success(compact('posts'));
}
}
Then I created a route in the file api.php:
Route::get('posts', 'PostsController#index');
I ran the command
$ php artisan serve`
and I tested the URL
localhost:8000/api/posts
This error occurs:
BadMethodCallException
Method Illuminate\Routing\ResponseFactory::success does not exist.
file: vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php
line: 100
throw new BadMethodCallException("Method {$class}::{$method} does not exist.");
I can't understand why this happened. Please help me.
There is no success method on the ResponseFactory. You can find the available methods here.
You are calling a macro function that is not registerd to the responseFactory.To use success method,Create your custom responseServiceProvider and write this inside boot()
Response::macro('success',function($data){
return Response::json([
'data'=>$data,
]) ;
});
And then register your ResponseServiceProvider to app.php by adding your Class name to the array called providers. This is how you add to the array
App\Providers\ResponseMacroServiceProvider::class
According to the Codeignitor docs here: http://ellislab.com/codeigniter/user-guide/general/hooks.html it states:
pre_controller
Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
However, if I create a hook pre_controller hook with:
$hook['pre_controller'][] = array(
'class' => 'tester',
'function' => 'test',
'filename' => 'tester.php',
'filepath' => 'models',
//'params' => array('beer', 'wine', 'snacks')
);
and the file tester.php is:
class tester extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->library('migration');
}
public function test()
{
echo "hi";
exit;
}
}
I get this error:
Fatal error: Class 'CI_Model' not found in ******.php
Why is it not loading CI_Model? If I put a require_once('system/core/Model.php'); in the hooks.php file above the pre_controller definition, I get this error:
Fatal error: Call to a member function library() on a non-object in ****.php
Since it's not actually loading the CI_Model, functions such as library() would not work. How can I force it to bootstrap the CI_Model.
The first person that says "Use post_controller_constructor" will be shot on sight as that does not answer the question. I need it to load BEFORE it runs any constructor functions from the controller classes. I need access to extend the CI_Model class from the pre_controller hook.
The short answer is that CodeIgniter doesn't work the way you want it to. At the stage that you're trying to access a model, CodeIgniter hasn't loaded the required classes and it isn't available. Depending on exactly what you're trying to achieve, there may be another way to achieve this - without using a hook/using a later hook?
Viewing /system/core/CodeIgniter.php will show when each hook is called and when other tasks are performed; loading routing, loading global functions etc.
If you insist on using this hook, then you could add this: load_class('Model', 'core'); at the top of your model file (before you declare the class), but this would be a very dirty fix.
Make sure your class names follow the correct naming convention - tester should be Tester.
Edit: as you want to run the same code on every call (assuming every controller call), this is a possible solution:
Extend the core controller, and use this new controller as a base controller for all other controllers (as they will be sharing the same functionality). In the constructor of this new base controller, add the functionality that you want to run on every call. The code in this constructor will be called before any other code in any of your controllers.
Create the following file, application/core/MY_Controller.php.
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
// Do whatever you want - load a model, call a function...
}
}
Extend every controller in application/controllers, with MY_Controller, rather than CI_Controller.
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
// Your controllers other functions...
}
/*application/config/hooks.php*/
$hook['pre_controller'][] =
array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('beer', 'wine', 'snacks')
);
/*application/config/config.php*/
$config['enable_hooks'] = TRUE;
/hooks/Myclass.php/