BindingResolutionException: Target class [path\to\class] does not exist (Laravel 6) - php

I'm using Laravel 6 and php 7 along with ReactJS. I'm following Build a Basic CRUD App with Laravel and React tutorial to learn how to make a CRUD app. In the tutorial, it says that I need to enable CORS so the API can be accessed from the front-end application. After I install the barryvdh cors by running:
composer require barryvdh/laravel-cors
and add it to my Kernel.php:
protected $middlewareGroups = [
'web' => [
...
\Barryvdh\Cors\HandleCors::class,
],
'api' => [
...
\Barryvdh\Cors\HandleCors::class,
],
];
the class is still undefined in Laravel and I get the error shown here
Here is a screenshot of my code.
Does anyone know how to solve this issue?

It looks like you're using an out of date tutorial. If you Google for barryvdh/laravel-cors, you'll see that the repository has been renamed to fruitcake/laravel-cors.
composer require fruitcake/laravel-cors
I suspect that the dependency therefore didn't install, or if it did, that you're referencing the incorrect namespace. It should be:
\Fruitcake\Cors\HandleCors::class

Related

Laravel - Nexmo not picking up credentials in .ENV file

I recently added laravel/nexmo-notification-channel to my laravel project which also installed Nexmo/nexmo-laravel.
After installing, I published vendor files so that I get config/nexmo.php and in there I noted that it looks in the .env file for NEXMO_KEY and NEXMO_SECRET.
So I went ahead and created these within my .env file
NEXMO_KEY=[my_key]
NEXMO_SECRET=[my secret]
NEXMO_SIGNATURE_SECRET=[my signature secret]
After this, I added Nexmo to my service providers in app.php:
'providers' => [
...,
Nexmo\Laravel\NexmoServiceProvider::class
]
and also added the following in config/services.php:
'nexmo' => [
'key' => env('NEXMO_KEY', ''),
'secret' => env('NEXMO_SECRET', ''),
'sms_from' => '[my number]'
],
But I still get the following error when thrying to send an SMS using the use Illuminate\Notifications\Messages\NexmoMessage; class:
"message": "Provide either nexmo.api_secret or nexmo.signature_secret",
I can use these same credentials to send an SMS from CLI, so why can't I send it from laravel?
There have been a couple of workarounds for this that are valid, but at first glance it looks like the Nexmo package does the work to bring in the ENV secrets into Laravel's config. Because of caching problems, you should never call env() within Laravel, instead you should be using config() - so in this case, config(nexmo.api_secret).
My main point here though is that I can't look into the "correct" solution for you because the package is abandoned. Nexmo is no longer Nexmo, it's Vonage, and Laravel core team have subsequently updated the notification-channel package.
For supported use to integrate Vonage services (SMS), please use the following package:
https://github.com/laravel/vonage-notification-channel
I'm not sure exactly why, but, Vonage/Nexmo doesn't pick details from the .ENV.
Instead, use a global constant to fetch the secrets:
Create a global.php file in the config folder, and add your secrets from the env like this:
<?php
return [
// Other constants values
'SMS_API_KEY' => env('SMS_API_KEY', ''),
'SMS_API_SECRET' => env('SMS_API_SECRET', ''),
]
?>
Then, you can use the constants in your controller as usual:
'key' => config('global.SMS_API_KEY'),
'secret' => config('global.SMS_API_SECRET')
then: recache, php artisan config:cache

prevent api to be called from anywhere in laravel application

i have a laravel application on version 5.7 . the problem is that my api is being called from outside of my server and people can easily call my api and send SMS and cause my some amount of charge . now what I want to do is that prevent the api from all locations to be called just my own server . I heard that laravel 7 has the cors configured but I wanted to know if there is any way for laravel 5.7 to do that . i have throttle for my api but it seems that its not working or the atacker changes the ips of servers .
so here is my kernel.php :
'api' => [
'throttle:1000,1',
'bindings',
],
'apiThrottle' => [
'throttle:4,10',
],
];
You can install https://github.com/spatie/laravel-cors/ package. That will create a file called cors.php under config folder. Follow all the setup instructions, are few.
There you can specifiy which origins are allowed (example)
'allow_origins' => [
'http://127.0.0.1/*',
'https://127.0.0.1/*',
'http://localhost/*',
'https://localhost/*',
],

Use Guzzle in Laravel package development

Recently i am working on a package for Laravel.
I am at beginner level on Laravel Package Development.
I need to use Guzzle Client for an API request from this package.
If i use use GuzzleHttp\Client; in my controller, it's showing class not found. (I know why. because it's outside of app folder and not autoloaded for this path)
Now, how can i use guzzle inside of my custom package controller.
Here what i wanting:
This is my controller method:
public function packageList($vendor, $type)
{
$client = new Client();
$result = $client->get('https://packagist.org/packages/list.json?vendor='.$vendor.'&type='.$type, [
'form_params' => [
'sample-form-data' => 'value'
]
]);
dd($result);
}
I attached classes of Guzzle
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
Getting Exception:
Class 'GuzzleHttp\Client' not found
Found this answer on stack with the same question
Open up your terminal at the root of your project and enter
composer require guzzlehttp/guzzle
It worked for the mailgun API. For some reason, the suggested method at the laravel's mail doc. You may install this package to your project by adding the following line to your composer.json file
"guzzlehttp/guzzle": "~5.3|~6.0"
doesn't make the composer download the Guzzle source codes. By the way, I didn't find out what | means in determining the version. This command just downloads the PSR code.
In this moment, the solution may work. However, be aware of compatibility issues. Because the command would install the latest stable version, not the suitable one.
answer by: shampoo
if this doesnt help, try reading the docs : http://docs.guzzlephp.org/en/stable/quickstart.html

Yii2 Class dektrium\user\Module does not exist

I have designed Yii2 powered blog by creating models, views and controllers but when I integrate user registration as explained in this tutorial https://code.tutsplus.com/tutorials/how-to-program-with-yii2-integrating-user-registration--cms-22974 I get:
Class dektrium\user\Module does not exist
I fail to solve this error anyone who can direct me, please.
I think you need to include the required module via composer:
"dektrium/yii2-user": "*"
Then run the "composer update" command in your console.
Mabe you need to add to config:
'user' => [
'class' => 'dektrium\user\Module',
'admins' => ['MegaAdmin'],
],
If you have it in common/config/main.php try to replace to backend/config/main.php

Laravel event:generate only creates Event and returns error

I'm using Laravel 5.3, I'm following the example here
https://laravel.com/docs/master/events#registering-events-and-listeners
I create the following in my EventServiceProvider
protected $listen = [
'App\Events\UserWasRegistered' => [
'App\Listeners\AddRoleForRegisteredUser',
],
];
When I then run
php artisan event:generate
It's only generating the event and throwing the following error
[Symfony\Component\Console\Exception\LogicException]
An option with shortcut "q" already exists.
Any idea what I might be doing wrong?
Thank you!
That's a Laravel bug, already fixed. Simply run composer update to update the Laravel framework and all should be ok.

Categories