I just read the Laravel 5 doc on configuration based on your environment http://laravel.com/docs/5.0/configuration#environment-configuration
I understand how the .env files can be used to configure some variables but how do I configure what service providers get loaded based on the environment?
For example in my ''local'' environment I use this debugbar plugin which needs a service provider but I don't want to define it in config/app.php for production.
In Laravel 4.2 I could just create config/local/app.php but that doesn't seem to work anymore, what is the alternative?
This article was helpful for me:
https://mattstauffer.co/blog/conditionally-loading-service-providers-in-laravel-5
And this is what I've done and it works:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if ($this->app->environment('local')) {
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
}
}
}
I believe if you set APP_DEBUG to false in your .env file the debugbar will go away automatically.
Also I wrote a middleware class to take care of that.
The handle method could look like this in your case:
public function handle($request, Closure $next)
{
if (app()->environment('production'))
\Debugbar::disable();
else
\Debugbar::enable();
return $next($request);
}
Then you just add it to the list of middleware in app/Http/kernel.php and you're good to go.
I have some additional conditions and more environments so this exact code is not tested but it should work and you should get the idea.
Take care.
Related
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');
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.
Hello Developers & Coders ,
My question is How to define a global variable , that can be used in
all controllers in Laravel ?
I have defined one variable $company in AppServiceProviders's boot method - that im using in all blade views , but I can not use it in controllers file , it gives error , undefined variable $company
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
View::share('key', 'value');
Schema::defaultStringLength(191);
$company=DB::table('company')->where('id',1)->first();
View::share('company',$company);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
please guide me , thanks for your efforts & time :)
set configuration variables at runtime
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
View::share('key', 'value');
Schema::defaultStringLength(191);
$company=DB::table('company')->where('id',1)->first();
// View::share('company',$company);
config(['yourconfig.company' => $company]);
}
}
usage:
config('yourconfig.company');
Okay, so unless you want to keep it in your Session, which I absolutely do not recommend, Cache which does not seem to be a best idea either or set it through the Config system inside of a Framework (which already are 3 different solutions suited for different matters) I would start from thinking what that variable will contain, if that's something that's just a Collection of Company model then you can basically use it in any controller by just using Laravel Eloquent methods.
What I recommend is either $company = Company::where('foo', 'bar')->first();, or just some data provider that would return all the information that you need in the form of Laravel Collection.
tl;dr
#recommended way that's reusable throughout whole app
Company::find(1); // instead of DB::table('company')->where('id', 1)->get();
Hope that helps.
I did see the SO warning that The question you're asking appears subjective and is likely to be closed when I posted this.
I continued posting it because it is from an authoritative source... which is the actual Laravel documentation.
I am asking what they mean and what examples can be given for the warning that is given(see below).
I was reading the documentation on service providers and found the following:
Writing Service Providers
All service providers extend the Illuminate\Support\ServiceProvider
class. Most service providers contain a register and a boot method.
Within the register method, you should only bind things into the
service container. You should never attempt to register any event
listeners, routes, or any other piece of functionality within the
register method.
Source is here
No example is given and I do not quite get what they mean by that statement.
The answer may have been answered elsewhere, in which case I apologise as I didnt find it.
What do they mean? Could you give an example of what not to do?
After all of the providers have been registered, they are “booted”.
A common mistake when using service providers is attempting to use the services provided by another provider in the register method. Since, within the register method, we have no guarantee all other providers have been loaded, the service you are trying to use may not be available yet.
So, service provider code that uses other services should always live in the boot method. The register method should only be used for, you guessed it, registering services with the container. Within the boot method, you may do whatever you like: register event listeners, include a routes file, register filters.
bind('servicName',function($app){
return 'your code logic';
})
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
app()->bind('servicName',
function($app){
return dd('your code logic');
});
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
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);
}
}