Laravel 5 view composer service provider, exclude prefix from wildcard - php

Following the view composer documentation I've created a class ComposerServiceProvider and a ViewComposer for my basic views. I want to use another view composer for the administration area of my site called AdminComposer. These are my class headers:
(namespace App\Http\Composers)
class ViewComposer
class AdminComposer extends ViewComposer
This is my Composer Service provider:
<?php namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
/**
* Register bindings in the container.
*
* #return void
*/
public function boot()
{
View::composer('admin/*', 'App\Http\Composers\AdminComposer');
View::composer('*', 'App\Http\Composers\ViewComposer');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
In the method boot() of my composer service provider I want to assign the ViewComposer to all my views, except for the ones with the prefix "admin", which should use AdminComposer.
In the current setting however, both view composers are used, as path "admin/*" adheres to path "*/" as well. Is there a way I can exclude the admin prefix from the wildcard path, without having to individually declare all paths that will use ViewComposer instead?

It's probably enough to only attach the view composers to your two layout files. This way you not only resolve the conflict between normal and admin views but also the composer runs just once per request and not twice or more (for the view and the layout view and possibly more views)
View::composer('layouts.admin', 'App\Http\Composers\AdminComposer');
View::composer('layouts.master', 'App\Http\Composers\ViewComposer');

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');

How to create aliases in laravel?

I want to create aliases in laravel like Auth so that i can use it in view just like
Auth::user().
For example, i want to return data from my setting table and want to use it like
Setting::method()->value. in view and use Setting in controllers.
What should i create for this Facades Or Service Providers? Provide me some procedures.
I tried using Service Providers but i am confused how to call database there.
This will be a long answer, using Laravel 5.4.
You need a service class (in this case, it's a concrete class behind the facade). I made it inside app/Services folder:
namespace App\Services;
use Illuminate\Database\DatabaseManager;
class Setting
{
protected $db;
public function __construct(DatabaseManager $db)
{
$this->db = $db;
}
public function method()
{
return;
}
}
As you see, I inject the database inside that concrete class. This is the magic of Laravel IoC. It will automatically resolve any dependency into your concrete class.
Next, create a facade. I made it inside app/Facades folder:
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Setting extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'setting';
}
}
Notice the setting string returned from getFacadeAccessor. You need to register this service name later to the Container (IoC). The magic behind this facade is, it's automatically call (proxy) any static method to the instance method of concrete class, in this case you can call:
Service::method()
Register the service to the container. Inside your AppServiceProvider
namespace App\Providers;
use App\Services\Setting;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
// ...
/**
* Register any application services.
*
* #return void
*/
public function register()
{
// ...
$this->app->singleton(Setting::class);
$this->app->alias(Setting::class, 'setting');
}
}
In this Service Provider, I declare that Setting service is a singleton service. Which means, you can use Setting service in another place without re-initialize class, in another words you are using the same instance across file. Last, I tell the container that Setting service has another alias, name setting, so Container can figure out which Concrete Class behind App/Facade/Setting.
For aliasing, as mentioned by apokryfos. Register your facade alias inside config/app.php, find the section aliases and add this line:
'aliases' => [
// ...
'Setting' => App\Facades\Setting::class,
],
After this you can call your facade like this:
use Setting;
Setting::method();
Hope this helps.

Laravel / Lumen - Reflection Exception Class does not exist

I am trying to inject a Manager class into toe Service Container of Lumen. My goal is to have a single instance of LogManager which is available in the whole application via app(LogManager::class).
Everytime i try to access this shortcut i get the following exeption:
[2017-03-23 16:42:51] lumen.ERROR: ReflectionException: Class LogManager does not
exist in /vendor/illuminate/container/Container.php:681
LogManager.php (i placed that class in the same location where my models are (app/LogManager.php))
<?php
namespace App;
use App\LogEntry;
class LogManager
{
...
}
AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\LogManager;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->singleton(LogManager::class, function ($app) {
return new LogManager();
});
}
}
I uncommented the line $app->register(App\Providers\AppServiceProvider::class); in bootstrap/app.php
I think that i missed something with the correct namespacing or placement of the classes espaccially LogManager. Maybe some one is willing to give me a hint?
If you need some more informations just give me a hint!
Your class and your service provider look fine. However, wherever you're calling app(LogManager::class) also needs to know the fully qualified name of the class.
Either make sure you have use App\LogManager at the top of the file, or change your call to app(\App\LogManager::class).

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