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()
{
//
}
}
Related
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.
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.
I'm using PHPStorm and I'm learning Laravel.
For "key too long" error I'm following the fix here: https://laravel.com/docs/master/migrations#creating-indexes
But, PHPStorm complaints for
Method 'defaultStringLength' not found in \Illuminate\Support\Facades\Schema
Why and how can I Solve? This is my AppServiceProvider.php file
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
/**
* Fix for key too long.
* #see https://laravel.com/docs/master/migrations#creating-indexes
*/
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I faced the same problem once and followed the answer from the following link to solve my problem. https://stackoverflow.com/a/44859379/4437710
Basically, replace use Illuminate\Support\Facades\Schema; with use Schema; I don't know the reason for this strange behavior. I'm not sure if it will work on your case too. But you can give it a try.
Another trick taken from the internet (Not tested by me):
For Laravel 5.4, use \Illuminate\Database\Schema\Builder::defaultStringLength(191); for correct function reference path instead
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
I've bound an interface to its implementation, like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Mail\Contracts\Webhook;
use App\Services\Mail\Clients\MailgunWebhook;
class MailServiceProvider extends ServiceProvider {
protected $defer = true;
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot() {
//
}
/**
* Register the application services.
*
* #return void
*/
public function register() {
$this->app->bind(
Webhook::class,
MailgunWebhook::class
);
}
}
I ran all of these:
php artisan config:clear
php artisan clear-compiled
php artisan optimize
composer dumpautoload
Yet, I still got the "Target is not instantiable error", when trying to use the binding.
After I commented out the $defer property, the binding started to work.
Why can't I use $defer in this case?
As stated in the documentation, to which I linked myself and failed to read it in full, $defer needs the provides() method.
In my case, all I needed to add in my service provider class is this:
public function provides() {
return array(Webhook::class);
}
Thanks to James Fenwick for his comment.