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

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.

Related

Laravel5.6 - override a vendor view

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

Laravel vendor:publish directory permission issues

I'm building a package that needs to publish to these directories when php artisan vendor:publish is called.
Directories includes
public_path('vendor/package/assets')
resources_path('vendor/package/views')
resources_path('vendor/package/translations')
I get permission related error when I run vendor:publsih which tries to write to these directories.
However vendor:publish works for database_path('migrations').
Laravel requires permission 775 for storage and bootstrap/cache.
I don't want package users to be forced to give write permission to public and resources directories.
NB:My package already uses $this->loadViewsFrom, $this->loadTranslationsFrom and $this->loadMigrationsFrom as default options. I wish to give my package users full access to modify views, translations, migrations etc. My package also needs to publish some assets to public directory such as js, css etc, which are very necessary for some views.
What am I missing about artisan vendor:publish, and how do I get this to work?
In your package's service provider boot method, you should rely on the loadViewsFrom and loadTranslationsFrom methods to properly include views and translations files:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'courier');
$this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'courier');
}
More information about including views can be found here and translations here.
If you are looking to publish the files instead of just loading them for use, then use the publishes method:
public function boot()
{
$this->publishes([
__DIR__.'/path/to/assets' => public_path('vendor/courier'),
]);
}

Laravel admin panel for existing project

I am now developing a project with Laravel 5.4
I need to add an Admin Panel over this existing project, but from what I see, there are quite few options for that.
One option is AdminLTE, but documentation on installing over existing project is very undetailed.
It also requires me to delete default Laravel Auth Controller, which is really not an option for me, because I've done a lot of changes in it.
Can you please recommend any admin panel that would be easy to install on existing project ?
Or should I write it myself ? But I'm not sure I can handle it.
i can recommend you a voyager package for laravel 5
step:1 composer require tcg/voyager
step:2 set up your .env file
step:3 put following 2 line in your app.php
TCG\Voyager\VoyagerServiceProvider::class,
Intervention\Image\ImageServiceProvider::class,
step:4 php artisan voyager:install
step:5 run your migration -> php artisan migrate
and then go to
localhost:8000/admin
or whatever your server is
Here I would recommend you to adopt a Laravel admin panel which I am using it already. That was the best Admin template which I've seen in recent days. Name of the product is Josh and available on Codecanyon.
Here is the link of the product where you can get it
https://codecanyon.net/item/josh-laravel-admin-template-front-end-crud/8754542?s_rank=9
You don't even need to follow the installation instruction provided by AdminLTE. As its a frontend template, just copy the css and js files of AdminLTE to your Laravel's public folder. Then take the html pages that you need from AdminLTE. You need to update the css and js links of that html with the new path of those file in your Laravel(use asset method).
Supposition: You are with Laravel 5.4
Add admin-lte Laravel package with:
composer require "acacha/admin-lte-template-laravel:4.*"
To register the Service Provider edit config/app.php file and add to providers array:
Acacha AdminLTE template provider
Acacha\AdminLTETemplateLaravel\Providers\AdminLTETemplateServiceProvider::class,
To Register Alias edit config/app.php file and add to alias array:
Acacha AdminLTE template alias
'AdminLTE' => Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::class,
Finally Publish files with:
php artisan vendor:publish --tag=adminlte --force
Use force to overwrite Laravel Scaffolding packages. That's all! Open the Laravel project in your browser or homestead machine and enjoy!

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