Even the controller is in the right folder an has the right name, when i test it in thunder client i get:
{ "error": "Error en la aplicación
(Illuminate\Contracts\Container\BindingResolutionException):Target
class [LoginController] does not exist." }
Here's my controller in app/http/Controllers/api
<?php
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use App\Models\Usuario;
class LoginController extends Controller
{
public function login(Request $request)
{
$usuario = Usuario::where('login', $request->login)->first();
if (
!$usuario ||
!Hash::check($request->password, $usuario->password)
) {
return response()->json(
['error' => 'Credenciales no válidas'],
401
);
} else {
$usuario->api_token = Str::random(60);
$usuario->save();
return response()->json(['token' => $usuario->api_token]);
}
}
}
and here's my route in the folder routes/api
Route::post('login', [LoginController::class, 'login']);
i have tried changing the name, i changed the route to
Route::post('login', [\App\Http\Controllers\Api\LoginController::class, 'login']);
and then said
{ "error": "Error en la aplicación
(Symfony\Component\ErrorHandler\Error\FatalError):Cannot declare
class LoginController, because the name is already in use" }
You are missing your namespace :
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
[...]
Related
How can I resolve this error? I am trying to customize the default email template on laravel. This is the code for the controller that sends the email.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use App\Models\User;
use Illumunate\Auth;
class EmailVerificationNotificationController extends Controller
{
public function store(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);
}
Mail::send('email.template', $request->user(), function($mail) use($data){
$mail->to($request->user()->email, 'no-reply')->subject("Verify Email Address");
$mail->from('admin#raketlist.com','testing');
});
$request->user()->sendEmailVerificationNotification();
return back()->with('status', 'verification-link-sent');
}
}
Add use Illuminate\Support\Facades\Mail; to other uses.
when trying to get data and returns it via transformers got an error Class 'App\\Api\\V1\\Controllers\\Auth\\ApiController' not found. Using ApiController to extends.I put APIcontroller to App\\Api\\V1\\Controllers\\Front folder.
LoginController Code and extends it to ApiController:
<?php
namespace App\Api\V1\Controllers\Auth;
use Auth;
use Carbon\Carbon;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\User;
use App\UserDeviceData;
use Illuminate\Http\Request;
use App\Transformers\UserTransformer;
class LoginController extends ApiController // extends to API controller
{
public function login(LoginRequest $request, JWTAuth $JWTAuth)
{
$credentials = $request->only(['email', 'password']);
try {
$token = Auth::guard()->attempt($credentials);
if(!$token) {
return response()->json([
'message' => "Email and password do not match",
'status_code' => 403,
]);
}
$user = Auth::user();
$user->last_login = Carbon::now();
$user->save();
$user = Auth::user();
$user->UserDeviceData()->firstOrCreate([
'device_id' => $request->device_id
]);
} catch (JWTException $e) {
return response()->json([
'message' => "Internal server error",
'status_code' => 500,
]);
}
return $this->response->collection($user, new UserTransformer);
}
}
Api controller code : and set namespace App\Api\V1\Controllers\Front;
<?php
namespace App\Api\V1\Controllers\Front;
use App\Support\Response;
use App\Support\Parameters;
abstract class ApiController extends Controller
{
protected $response;
protected $parameters;
public function __construct(Response $response, Parameters $parameters)
{
$this->response = $response;
$this->parameters = $parameters;
}
}
What is the problem in my code?
As ApiController and LoginController are using a different namespace, you need to use your ApiController in LoginController.
use App\Api\V1\Controllers\Front\ApiController;
You also forgot to use Controller in ApiController:
use App\Http\Controllers\Controller;
I am new to Laravel. The Laravel app I am creating works perfectly on my local machine, but when I uploaded it on my host the Auth session is not persisting on other routes.
routes.php
Route::group([], function () {
Route::group(['domain' => 'admin.domain.com'], function() {
Route::get('/login', 'LoginController#login');
Route::get('/checklogin', 'CheckLoginController#check');
});
});
LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\User as User;
class LoginController extends Controller
{
//
public function login()
{
$user = User::find(1);
Auth::login($user);
return "logged in"; //returns logged in
}
}
CheckLoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
class CheckLoginController extends Controller
{
//
public function check()
{
if( !Auth::guest() ) :
echo "logged in";
else :
echo "Not logged in"; //returns this
endif;
}
}
I have ChatController located in app/http/controllers like so:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use DB;
class ChatController extends Controller implements MessageComponentInterface {
protected $clients;
function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
foreach ($this->clients as $client)
{
if ($client !== $conn )
$client->send($msg);
DB::table('messages')->insert(
['message' => $msg]
);
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'the following error occured: ' . $e->getMessage();
$conn->close();
}
}
And I have chatserver.php file in the root like so:
<?php
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Http\Controllers\ChatController;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatController()
)
),
8080
);
$server->run();
If I remove
DB::table('messages')->insert(
['message' => $msg]
);
from the ChatController and start chatserver.php it works, but if I don't remove it then the server starts but as soon as I send a message I get this error:
Fatal error: Uncaught Error: Class 'DB' not found in C:\wamp\www\laraveltesting\app\Http\Controllers\ChatController.php:31
Why won't it use DB? I am extending the laravel controller.
This one is better
use Illuminate\Support\Facades\DB;
Or you can use a slash('/') before DB like below
/DB::table('messages')->insert(
['message' => $msg]
);
As previously advised First use
use Illuminate\Support\Facades\DB;
then go /bootstrap/app.php and uncomment
$app->withFacades();
try using this
use Illuminate\Support\Facades\DB;
instead of
use DB;
for Laravel 5 and up simply just use this
use DB;
instead of
use Illuminate\Support\Facades\DB;
which is use for Laravel 4 version
change use Illuminate\Support\Facades\DB; to Use DB;
i am trying to use socialite for make "Login with facebook" but i am getting this error continuously BindingResolutionsException Target[Laravel\Socialite\Contracts\Factory] is not instantiable. Please help me.
'providers' => [
....
Sun\Flash\FlashServiceProvider::class,
Laravel\Socialite\SocialiteServiceProvider::class
],
'aliases' => [
....
'Flash' => Sun\Flash\FlashFacade::class,
'PmhAuth' => app\Library\Auth\PmhAuth\PmhAuthFacades::class,
'Socialite' => Laravel\Socialite\Facades\Socialite::class
],
here is my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
class SocialAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
public function callback()
{
}
}
Since you've aliased Socialite in your config:
'Socialite' => Laravel\Socialite\Facades\Socialite::class
Try importing the alias:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Socialite;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class SocialAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
public function callback()
{
}
}