Laravel webwizo shortcodes package Shortcodes class not found - php

I recently started using laravel so i'm a beginner, and right now i'm working on a small project which requires me use shortcodes(like the ones in wordpress).
So i searched for a little bit and found this package:
https://packagist.org/packages/webwizo/laravel-shortcodes
I ran the installation and usage the way it's written but i get the error : Class 'App\Providers\Shortcode' not found in the provider I have to make using the laravel make:provider command as specified in the package instructions, below is my exact usage and install code.
added this to the providers array :
/*
* shortcodes providers
*/
Webwizo\Shortcodes\ShortcodesServiceProvider::class,
App\Providers\ShortcodesServiceProvider::class,
Added this to aliases:
'Shortcode' => Webwizo\Shortcodes\Facades\Shortcode::class,
this is the content of my ShortcodesServiceProvider in app/providers:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
class ShortcodesServiceProvider extends ServiceProvider
{
/**
Bootstrap the application services.
*
#return void
*/
public function boot()
{
//
}
/**
Register the application services.
*
#return void
*/
public function register()
{
Shortcode::register('jobs', JobsForm::class);
}
}
I use laravel 5.4 so that might be an issue.
The thing is the class obviously exists, it gives the Shortcodes class not found error because I think it searches for it in the app/providers/ShortcodesServiceProvider file, and obviously it's not there it's in the vendor file.
Is there something I'm missing i've checked and double checked, I can't seem to get this thing to work.
It shoould work considering it has an alias defined right ?
I used it in the view like this:
return view('quarx-frontend::pages.' . $page->template)->with('page', $page)->withShortcodes();
Thanks for taking the time to read this any help would be much appreciated.
If you need any more info I'll be glad to supply it.
p.s. sorry for bad english ,not a native speaker :P

It searches for Shortcode in the App\Providers; namespace and not in the root namespace where the Facade is defined.
You can fix this in App\Providers\ShortcodesServiceProvider.php by either doing:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
use Shortcode;
class ShortcodesServiceProvider extends ServiceProvider
{
Or use \Shortcode
/**
* Register the application services.
*
* #return void
*/
public function register()
{
\Shortcode::register('jobs', JobsForm::class);
}
I would recommend the first option.

Related

Change app.blade.php destination (Jetstream)

So, I'm exploring the world of Jetstream and Fortify. Problem is that I want to change the default app.blade.php to my folder admin/app.blade.php.
Where is this defines so I can change it? Can't find it anywhere.
Many thanks!
(Ps: Don't know if this is categorised under Laravel of Vue.)
Update: To avoid confusion. I installed a fresh installation of Laravel with Jetstream. I'm using Inertia for developing my CMS. Inertia has a default file in /views. This is called app.blade.php, which calls #inertia. I want to move this file to /views/admin. If I do that, I get this error:
View [app] not found.
Which makes sense, because I moved it. Where do I change the render of app.blade.php to change it to admin/app.blade.php?
UPDATE:
You can register this in your AppServiceProvider.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if (request()->is(['admin', 'admin/*'])) {
Inertia::setRootView('admin.app');
}
Inertia::setRootView('app');
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}```
do like this
YourProjectPath/config/view.php
'paths' => [
resource_path('views/admin'),
],
if in Controller you can do like this
return view('admin.app');

Laravel View Composer, cant seem to get it to work

so i have followed many tutorials and videos and checked here and cant seem to get this to work, really need help. im going to provide my code and explain what im trying to so and hopefully someone can help me sort this out. so what im trying to so is to pass my $user and $company data from my database to my partial layout though view composer. i used php artisan to make a composerserviceprovider and and dashboardcomposer in my view/composer folder and i get errors.
my composerserviceprovider.php is as follows
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\companies;
use App\user;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
view()->composer("layouts.dashboard","App\View\Composers\dashboardnavComposer");
}
}
my dashboardnavcomposer.php is as follows
<?
namespace App\View\Composers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use App\companies;
use App\user;
class dashboardnavComposer
{
public function compose(View $view)
{
$view->with('user', ComposerServiceProvider::all());
}
}
im probably doing something completely wrong or completely missing a step, i already did the php artisan dumpautoload, so this is not the problem. any help would be appreciated. also to let you know, i use to do this 10+ years ago and i quit to be a truck driver and so last time i did this php4 was king. so please understand i just starting relearning this about 30 days ago.
thanks in advance for any help you can provide.
Not sure what you filename actually is for the composer but your filename should match the class name (exactly in case). The file should be named dashboardnavComposer.php. In reality the class should probably be more like DashboardNavComposer and the file would be DashboardNavComposer.php. Also make sure it is in the app/Views/Composers folder.
I am not sure why your View Composer is calling to a Service Provider, ComposerServiceProvider, though.
The PSR-4 autoloading should be picking up these new files. You can run composer dump just in case.

Laravel - Service Provider : Class not found

I started a project on Laravel 5.4 today and got a ServiceProvider problem.
Here my service provider :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class WizamProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//die('YESSS');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('Wizam\Test', function()
{
if(class_exists("Domains\Domomat\Test"))
return new \Domains\Domomat\Test;
else
return new \Core\Classes\Test;
});
}
}
I added this provider into config/app.php (App\Providers\WizamProvider::class), dump my autoloader like twenty times, clear cache, clear config. Nothing happened.
Here my route :
Route::get('/sub', function()
{
$test = new \Wizam\Test();
echo $test->render();
});
When I go to '/sub', I got Class 'Wizam\Test' not found. I cannot see my error, can you help me ?
Thanks !
To use the container you cannot instantiate using the new command, you can either inject it through the constructor
__constructor(\Wizam\Test $test)
{
}
or using the app(\Wizam\Test::class) I believe is the other way to do it as mentioned in the comments.
If someone once will get error saying that your interface not found ('class not found'), during making service provider closure, make sure your interface and class located in app/ folder. It took a lot of time fixing this. Have a nice day.
P.S. answer is not related to topic, but this page is first I found when I started to google the solution, so I beg you leave this here.

Why I can't find the "App\Extensions\RiakUserProvider" namespace in my Laravel project following this "Adding a custom provider" tutorial?

I am absolutely new to PHP and Laravel.
I am working on a Laravel 5.3 application and I have to use a custom web service to check the user credential so I am trying to follow this official tutorial about Adding a custom provider to handle user access: https://laravel.com/docs/5.3/authentication#adding-custom-user-providers. So, in theory it seems pretty simple but I am finding some difficulties.
As you can see in the previous tutorial as first step it modify the App\Providers\AuthServiceProvider class contained into the Laravel project.
So, I have modified my AuthServiceProvider according to the tutorial example obtaining this:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use App\Extensions\RiakUserProvider;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
// CUSTOM CODE:
Auth::provider('riak', function ($app, array $config) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
return new RiakUserProvider($app->make('riak.connection'));
});
}
}
The problem is that it can't find the App\Extension namespace, this:
use App\Extensions\RiakUserProvider;
PhpStorm signs Exstensions in red saying "Undefined Extensions namespace" so it can't use the RiakUserProvider class in my code.
Why? Do I have to add some dependencies in Composer? What is wrong? What am I missing? How can I fix this issue?
What exactly is the RiakUserProvider class?
What exactly does this code:
Auth::provider('riak', function ($app, array $config) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
return new RiakUserProvider($app->make('riak.connection'));
});
In the Laravel docs the RiakUserProvider class is just an example custom User Provider. Class is located in App\Extensions namespace, but the actual provider class content was not provided.
If you want to create a custom User Provider you should create a folder named Extensions in your App folder and create RiakUserProvider.php file containing RiakUserProvider class. This follows PSR-4 class autoloading standard.
When you create your own User Provider please make sure it implements Illuminate\Contracts\Auth\UserProvider interface.
Here is a nice step by step tutorial of creating one:
https://www.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers

Which provider do I use to bind my database repositories in laravel 5?

I am working with laravel 5 and I am novice developer so I just learnt how to use the Repository pattern. Now as novices go, I first use the routes.php file to bind my UserRepositoryInterface to DbUserRepository like so:
App::bind('UserRepositoryInterface', 'DbUserRepository');
This is just psuedo, image the namepspaces with the above code too.
So after this I realized that that there is something called a Service Provider which is supposed to contain code like so. Now I refactored this in the AppServiceProvider in my Providers folder and it works fine still.
But since I will be having so many more repositories, is this is a good idea to place them into the AppServiceProvider or should I go ahead and make a dedicated provider for my repositories and bind them there? What is the best way to do this? Is there a standard for this?
So later I got to understand that this all about preference so I coded a RepositoryServiceProvider in which I bind all the repository contracts to the desired implementations like so:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Archive\Repositories\TenantRepository;
use Archive\Repositories\TenantRepositoryEloquent;
use Archive\Repositories\UserRepository;
use Archive\Repositories\UserRepositoryEloquent;
use Archive\Repositories\OrderRepository;
use Archive\Repositories\OrderRepositoryEloquent;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind(TenantRepository::class, TenantRepositoryEloquent::class);
$this->app->bind(UserRepository::class, UserRepositoryEloquent::class);
$this->app->bind(OrderRepository::class, OrderRepositoryEloquent::class);
}
}

Categories