Laravel View doesn't recognize my service provider - php

I have created a service provider for the first time, to pass some data to my project view but i get an error message : Undefined variable: core Since i have already registred my new Service Provider in the config file correctly
App\Providers\CoreServiceProvider::class
This is my CoreServiceProvider.php :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Core;
class CoreServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
view()->composer('*', function($view){
$core = Core::all();
return $view->with('core', $core);
});
}
}
My index.blade.php
dd{{$core}}

if you are making a Custom ServiceProvider class it wont be loaded into the application.
You need to manually register those into Application.
You can register custom service provider under. providers array of config/app.php file.

Related

Class does not exist exception if I don't provide full path to my class in Laravel validation extension

I am using Laravel 5.8 and attempting to set up a custom validation extension.
I have created a class GroupValidator containing a validate function.
I have created a ValidationServiceProvider with the following code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
use App\Classes\GroupValidator;
class ValidationExtensionServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
Validator::extend('valid_parent_id', 'GroupValidator#validate');
}
}
When my validation triggers I get a Class GroupValidator does not exist exception. However if I specify the full path to my class in the extend function call like so:
Validator::extend('valid_parent_id', 'App\Classes\GroupValidator#validate');
then everything works fine.
Is there some way I can set this up so that I don't have to include the full path to my class?

Laravel Auth::check() always return false in AppServiceProvider.php

I'm trying to executing a function in every page and I do that in AppServiceProvider.php in boot() I dependent on Auth class but Auth::check() always return false
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Auth;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
dd(Auth::check());
}
}
From the Laravel Docs
Service providers are truly the key to bootstrapping a Laravel application. The application instance is created, the service providers are registered, and the request is handed to the bootstrapped application. It's really that simple!
Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.
and since Auth and Session are updated / initialized using a middleware, it means that you can't access to it from a Service Provider.
you can only bind data to views in your service providers using callbacks that are called when the view is rendered ( it means that the server is already preparing the response )
View::composer('is_authenticated', Auth::check());
Maybe Auth is not load on before AppServiceProvider. Because in Controllers Auth::check() work well. So i think using Auth::check() in AppServiceProvider is very bad idea. AppServiceProvider intended to register and bootstrap services, maybe there a better place for Auth::check() in you app?
you need view composer for this.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Auth;
use DB;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
view()->composer('*', function ($view)
{
if (Auth::check()) {
}
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
To correctly protect a route for only authorised users, you should use ->middleware('auth') on your route.
https://laravel.com/docs/5.8/authentication
Also I would suggest updating your question to include the Laravel version you are using.

Registering a custom controller class in Laravel

I have this this class that is a ServiceProvider
namespace Package\Avatar;
use Illuminate\Support\ServiceProvider;
class AvatarServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
include __DIR__.'/routes.php';
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
try{
$this->app->make('Package\Avatar\AvatarController');
} catch (\Exception $e){
dd($e);
}
}
}
But when I try to access to some url of AvatarCotroller class the screen is Blank, and no show neither error. But whenever I comment this line
$this->app->make('Package\Avatar\AvatarController');
I can get the normal errors of Laravel.
You can get rid of including the routes.php in the boot method of the service provider. Simply use $this->app->call('Package\Avatar\AvatarController#method') to call the method on the controller
try
php artisan optimize : to reuse all frequently used classes php will make an cached class in cache/service.php. So we if add new service we need to run it. We need to use it whenever we add new dependency without using composer.
php artisan cache:clear : clear all the above cache and remap everything

log every laravel action that happen?

where is the appropriate place to catch every time laravel is used even non http based action?
I want to catch everything even artisan commands, Queues or Task that running.
the only place I can think of is bootstrap\app.php
but its too hacky and with my experience with laravel I am sure there is some built in way of doing it
is there some one place to catch them all?
you can add your logger to your app/Providers/AppServiceProvider.php's boot() function.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
// Your logger goes here
error_log('log...');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}

Laravel 5.3, Class cant be found, While the route is correct

I am currently working on view composer:
The problem right now that i have, The route that im calling inside the ComposerServiceProvider.php says that it cant find the route to the ViewComposers/LespakketComposer.php
In the Config\app.php i did add the correct App\Providers\ComposerServiceProvider::class,
Here is my code in ComposerServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class ComposerServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer('*','App\Http\ViewComposer\LespakketComposer');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Here is my error:
ReflectionException in Container.php line 734:
Class App\Http\ViewComposer\LespakketComposer does not exist
The Routes in my folder structure
Does anyone has a solution for my problem?
( The file i am requesting is an Class Indeed )
You forgot a s:
App\Http\ViewComposers\LespakketComposer

Categories