Target class 'Uploadcare' does not exist - laravel-9

Executed:
composer require uploadcare/uploadcare-php
I can find the library in vendor/uploadcare.
Then added to config/app.php:
'providers' => [
App\Providers\UploadcareServiceProvider::class,
]
and
'aliases' => Facade::defaultAliases()->merge([
'Uploadcare' => App\Facades\Uploadcare::class,
])->toArray(),
Then in App/Facades folder, created this file:
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Uploadcare extends Facade {
protected static function getFacadeAccessor(){
return 'uploadcare';
}
}
This file is successfully referenced in config/app.php according to Visual Studio code.
Then in App/Providers I created UploadcareServiceProvider.php file:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Uploadcare\UploadcareConfiguration;
use Uploadcare\Interfaces\ConfigurationInterface;
class UploadcareServiceProvider extends ServiceProvider
{
public function boot()
{
$this->mergeConfigFrom(__DIR__.'/../../config/uploadcare.php', 'uploadcare');
}
public function register()
{
$this->app->bind(ConfigurationInterface::class, function ($app) {
return new UploadcareConfiguration();
});
$this->app->bind(Uploadcare\Api::class, function ($app) {
$config = $app->make(ConfigurationInterface::class);
return new Uploadcare\Api($config);
});
}
}
This file is successfully referenced in config/app.php according to Visual Studio code, it loads config/uploadcare.php which I have with the following (I redacted my public and private key):
<?php
return [
'public_key' => env('my-public-key'),
'private_key' => env('my-private-key'),
];
Then I created a controller to see whether I can successfully load a list of files form Uploadcare:
<?php
namespace App\Http\Controllers;
use Uploadcare\Api;
use App\Facades\Uploadcare;
use App\Uploadcare\UploadcareConfiguration;
use Uploadcare\Interfaces\ConfigurationInterface;
class UploadcareController extends Controller
{
public function index()
{
$api = Uploadcare::Api(config('uploadcare.public_key'), config('uploadcare.public_secret'));
$files = $api->files()->all();
return $files;
}
}
I call the controller using this route:
Route::get('/uploadcare', [UploadcareController::class, 'index']);
When visiting example.com/uploadcare I get the following error:
Target class [uploadcare] does not exist.
When I change the code in the controller to the following, I get the same error:
$files = Uploadcare::File()->getFileList();
When I hover Uploadcare in Visual Studio it successfully references the Facade.

Related

policy doesn't working in laravel in module

this is my Auth Service Provider :
<?php
namespace App\Providers;
use App\Models\Profile;
use App\Policies\ProfilePolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use RequestManager\Http\Models\RequestState;
use RequestManager\Policies\RequestStatePolicy;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Profile::class=>ProfilePolicy::class,
RequestState::class=>RequestStatePolicy::class,
];
public function boot()
{
$this->registerPolicies();
}
}
this is my policy array :
array:2 [
"App\Models\Profile" => "App\Policies\ProfilePolicy"
"RequestManager\Http\Models\RequestState" => "RequestManager\Policies\RequestStatePolicy"
]
I have a policy that is in its own module folder with this structure :
this is my controller :
public function showDepartmentRequests($id)//id = department id
{
$request=RequestState::where('department_id',$id)->get();
dd(\Gate::forUser(User::find(2))->allows('view',$request));
}
and this is my policy :
<?php
namespace RequestManager\Policies;
use RequestManager\Http\Models\RequestState;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class RequestStatePolicy
{
use HandlesAuthorization;
public function view(User $user, RequestState $requestState)
{
dd("test view policy");
}
}
Nothing is printed when I call the gate!
Is this error because I have to register a policy within a specific auth service Provider
other than the app\AuthServiceProvider?

notification system in Laravel 8

I am working on a laravel app where i have a user(organizer) who post an event ,and other users can comment on this event .
I am trying to make notifications system in laravel 8 between the organizer and the users when commenting on these event !
but i get this error (Call to a member function notify() on null).
This is my class :
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewCommentPosted extends Notification
{
use Queueable;
protected $user;
protected $event;
public function __construct($user, $event)
{
$this->user = $user;
$this->event = $event;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'user' => $this->user->name,
'eventTitle' => $this->event->title,
'eventId' => $this->event->id
];
}
This is storefunction() in my controller :
namespace App\Http\Controllers;
use App\Models\Event;
use App\Models\Comment;
use App\Notifications\NewCommentPosted;
use Illuminate\Http\Request;
class CommentController extends Controller
{
public function store(Event $event)
{
request()->validate([
'content' => 'required|min:5'
]);
$comment = new Comment();
$comment->content = request('content');
$comment->user_id = auth()->user()->id;
$event->comments()->save($comment);
$event->user->notify(new NewCommentPosted(auth()->user(), $event));
return redirect()->route('events.show', [$event->id, $event->title]);
}
Any help please !!?

Laravel controller based api routing

My normal web app runs w/o any issue. Then I wanted to experiment with APIs. I enabled Passport since I need api authorization (but at this moment, I rather want to get this thing working and I have no idea whether it is a problem with Passport) and I wanted to get simple json output of specific Product. So far, I was not able to get it working. I'll describe contents of each file and if someone can direct me to find the issue in my code, that would be great.
Resources\Product.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Product extends JsonResource
{
public function toArray($request)
{
return parent::toArray($request);
}
}
Providers\AuthServiceProviders.php
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
User.php
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
...
}
ProductController.php
class ProductController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function apiShow(Product $product)
{
return new ProductResource($product);
}
...
}
routes/api.php
Route::get('/products/{product}', 'ProductController#apiShow');
Now if I go to http://localhost/public/products/1, it displays the page as expected. But if I type in http://localhost/public/api/products/1, it will always go to home page which is set to localhost/public in HomeController.
If I modify routes/api.php as:
Route::get('/products/{id}', function($id) {
return Product::find($id);
});
I get the correct json output in the browser.

Using Policies in Laravel 5.5.14

I am new in using policies in Laravel. I am learning API Development using Laravel. My codes are as bellows.
TopicPolicy.php
<?php
namespace App\Policies;
use App\User;
use App\Topic;
use Illuminate\Auth\Access\HandlesAuthorization;
class Topicpolicy
{
use HandlesAuthorization;
public function update(User $user, Topic $topic)
{
return $user->ownsTopic($topic);
}
public function destroy(User $user, Topic $topic)
{
return $user->ownsTopic($topic);
}
}
AuthServiceProvider.php
<?php
namespace App\Providers;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
}
TopicController.php
<?php
namespace App\Http\Controllers;
use App\Topic;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests\StoreTopicRequest;
use App\Transformers\TopicTransformer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class TopicController extends Controller
{
public function destroy(Topic $topic) {
$this->authorize('destroy',$topic);
$topic->delete();
return response(null,204);
}
}
I am getting error This action is unauthorized.. I don't know how to use policies. Could anyone guide me to use policies in Laravel 5.5.14 ?
In the AuthServiceProvider class you have to register the policy in the policies aray. Laravel documents is a good place to start
protected $policies = [
\App\Topic::class => \App\Policies\Topicpolicy::class
]
Second obtain a personal token from your app so you can use it to make calls to your api. You use passport and passport comes with ready to use Vue components to help you start.If you want to consume the api inside the same application check here.
I am not sure what you try to accomplice with the HandlesAuthorization trait inside the policy. Laravel has a middleware for this reason for us to use.

Problem in Creating Simple Form in Zend

I am new on Zend Framwork(MVC). I want to crate simple a Form with some HTML control.
I have create one controller IndexController, code are as follows:
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
/*$this->view->var = 'User Login Page';*/
$form = new Form_Login();
$this->view->form=$form;
}
}
And my Form's code in application/forms/Login.php:
<?php
require_once('Zend/Form.php');
class Form_Login extends Zend_Form
{
public function init()
{
parent::__construct($options);
// the code bellow will create element
$username = $this->CreateElement('text','username');
$username->setLabel("Username:");
// and
$submit= $this->CreateElement("submit","submit");
$submit->setLabel("Submit");
// now add elements to the form as
$this->addElements(array(
$username,
$submit
));
}
}
?>
When i run this project then its show an error like this:
**Fatal error: Class 'Form_Login' not found in C:\xampp\htdocs\LoginForm\application\controllers\IndexController.php on line 16**
Please help me...
Thanks
Pankaj
Everything looks good, make sure your Login.php file has this as the first lines:
<?php
class Form_Login extends Zend_Form
{
public function init()
If that doesn't help, you might want to check your index.php/Bootstrap.php files and server configuration to make sure all paths are correct.