Laravel5.6 - override a vendor view - php

I try to make my own version of a vendor blade template.
I dont want to extends the controller with the reference of the view.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(__DIR__.'/../../resources/views/oauth/passport', 'passport');
I created a file named authorize.blade.php in /resources/views/oauth/passport
In the vendor controller method we can see this:
return $this->response->view('passport::authorize');
The problem is when I call the vendor controller method it loads his version of authorize.blade.php. I would like mine to be loaded and I expected the new line I added to AppServiceProvider to do that.

Passport comes with VUE components and views you need to publish first to override them. From the Laravel Passport page:
"If you would like to customize the authorization approval screen, you may publish Passport's views using the vendor:publish Artisan command."
All you need to do is run php artisan vendor:publish --tag=passport-views and the vendor views will be place in resources/views/vendor/passport, where you can edit them.

Use can use php artisan vendor:publish --tag=passport-views this will copy the views to your views folder for you to change.

So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(DIR.'/../../resources/views/oauth/passport',
'passport');
You can use this option only by placing it in registry () instead of boot(). And then you can use your Views regardless of whether they were published in Vendor or not

Related

Unable to list laravel routes because target class not found

I am currently working on an API only laravel application. In the controllers folder, there is an API folder that holds all controllers. The ForgotPasswordController is in the API folder as well.
When I run the command php artisan route:list I get the error below
Illuminate\Contracts\Container\BindingResolutionException : Target class [App\Http\Controllers\Auth\ForgotPasswordController] does not exist.
There is actually no ForgotPasswordController in the Auth folder. How do I handle this issue?
You have to make sure you are doing php artisan route:cache priority.
If the problem still persists, can you disable the auth provider and try?
For the sake of time considering that the project is live and I need to churn out a couple of features, I have moved both ForgotPassword and ResetPassword controllers back into the Auth folder. Ran a test to make sure nothing has been broken (everything works fine) and now I am able to list out the routes.
If you have Auth::routes() or Route::auth() in your routes file that would be generating routes to the ForgotPasswordController.
You would need to not be calling that or you would need to pass the proper option to it to have it not register those routes:
Auth::routes(['reset' => false]);
Depending on the version you are using this may not work. If that is the case you will have to not use this method at all and register the routes you want/need yourself.

Unable to Register View Composer for Laravel Backpack Dashboard View

I'm attempting to register a View Composer for the Laravel Backpack libraries dashboard template, which is located at resources/views/vendor/backpack/base/dashboard.blade.php within my project.
I'm doing so via
View::composer(
'vendor.backpack.base.dashboard', 'App\Http\View\Composers\DashboardComposer'
);
I know that the Composer is working correctly, because if I change the template to
View::composer(
'*', 'App\Http\View\Composers\DashboardComposer'
);
the dashboard page loads correctly with the variable I'm setting in the composer.
I've tested to see if the view reference is correct by using
if (View::exists('vendor.backpack.base.dashboard')) {
//
}
which returns successfully.
I'm registering my provider, App\Providers\ViewServiceProvider::class, within the config as the very last provider.
Does anyone know why my composer won't attach to the dashboard view?
Backpack namespaces their views. You can try defining the composer for the namespaced version of the view:
View::composer('backpack::dashboard', ...);

Is it possible to override a laravel package view from another package

Is it possible to override package view from another package?
When you register a view path via
loadViewsFrom('path/to/views', 'package')
it will also look in
/resources/views/vendor/package
So you can override a view when using a package,
but is there a way to override a view within a different package?
Yes it is.
Steps:
Publish the package views you want to override:
php artisan vendor:publish --provider="Another\Package" --tag=views
Modify the published ones.
Place the modifications in a dir in your package, e.g.:
/resources/vendor/another-package/views
Make it available for publishing. Add to your package's service provider boot():
public function boot()
{
$this->publishes([
__DIR__.'/resources/vendor/another-package/views' =>
base_path('resources/views/vendor/another-package')
], 'views');
}
Publish the modifications:
php artisan vendor:publish --provider="Your\Package" --tag=views --force
Note: change Another\Package and another-package as necessary. Works well in Laravel 7.

Laravel: Edit file in the vendor directory

I am working with laravel and I have installed a package using composer by running this command composer require mailchimp/mailchimp=~2.0.
After that I got a folder 'mailchimp' in the vendor directory. In there, there is a file named Mailchimp.php that I have to modify, but based on some old posts here, if I modify the file, any time I run the command composer update, I will loose my changes in the file, just because it is located in the vendor directory. So is there any option for me to solve this problem ?
I tried using the command php artisan vendor:publish but I do not get the expected results.
You can create a custom class which will extend the Mailchimp class and override the function you want. Then use the custom class in your code.
use DrewM\MailChimp\MailChimp;
class CustomMailChimp extends MailChimp {
...
// The function you would like to override
}
Then use it new CustomMailChimp(..)

laravel 4 workbench routing to the package

I have been working with laravel 4 for some time now and i needed to create an admin area so i decided to use a package to keep things all organized and separated from the rest of the application.
So i created a package with composer as 'vendor/admin'.
then i added those lines as documemented on laravel site
AdminServiceProvider.php
public function boot()
{
$this->package('vendor/admin', 'admin');
include __DIR__.'/../../routes.php';
}
public function register()
{
//
$this->package('vendor/admin');
}
I also created a routes.php file in vedor/admin/ directory to route all admin area in this file.
following i run the 'php artisan dump-autoload' and i finalized with this commend on artisan 'php artisan config:publish vendor/admin'
now i wanna be able use this package for mysite.com/admin route and i want the routes.php file in the package to render the routing for that URI, to do that:
Do i need to modify my app/routes.php?
How can i make vendor/admin/src/routes.php file to do the routing for all mysite.com/admin routes?
Thanks.
No you don't need to edit app/routes.php. As long as it doesn't contain any admin routes that could collide with the ones in your package you can leave it that way.
The routes file in a package can be used like the "normal" app/routes.php. An easy way to deal with admin routes is to have a prefix group:
Route::group(array('prefix' => 'admin'), function(){
// all your admin routes. for example:
Route::get('dashboard', '...');
// will match GET /admin/dashboard
});
Besides that, make sure you're package gets loaded correctly! One part being registering the service provider. Assuming the namespace of your package is Admin you need to add Admin\AdminServiceProvider to the providers array in app/config/app.php. More information

Categories