I am making a package for Laravel 5 that will use bootstrap 3 and jquery.
How to include these assets in my package properly?
What if a person who will use my package will already have bootstrap and jquery in his/her project? Is it necessary to check that?
I can put them in my folder assets of the package, but what if these assets are in the project already?
You can not check if the project already has these assets, but if you want to include your own as part of the package you can do so with a provider.
Make a provider and register it in config/app.php (if your package doesn't have one already) and inside the boot() method you can "publish" (copy) assets from your package folder into the public folder of the Laravel application.
$this->publishes( [
realpath( base_path( 'your/package/folder/with/assets' ) ) => public_path( 'desired/folder/in/public' )
], 'public' );
If you are using default files without any modifications. then it is better to use from online repository, As keeping it public may override users custom settings. and it is wise to keep the package as small as possible.
And also you cannot check the whole project of user for these files
Related
I have just started exploring laravel but I have one confusion . I know how to create own custom function file and make it available globally using compose.json file but I was trying to figure out how laravel's helper function like route() , view() are accessible without including there source file and I can't find any auto discovery in composer.json file neither in any Service Provider .
PS : I have only checked in in Providers/ Directory.
Can anyone tell me how this thing works?
Through composer Laravel has defined which files should be autoloaded. With the line in the composer.json file in Laravel/framework it specifies what should be autoloaded.
It loads the following file.
You can create similar autoloaders if you prefer, but having to much logic in such helpers could easily become an anti pattern. As the logic, is a little more hidden than class based logic, when people have to look through your projet.
On your composer.json file on the root directory of your laravel app, look for the entry autoload.
This means all methods on those directories are autoloaded.
That is why if you have (newly) created a method / function within those directory and it doesn't work (or not found) as expected, you need to run composer dump-autoload to make sure that everything has been loaded.
That's also where I put my custom helper file:
"files": [
"app/Helpers/helpers.php"
]
All function here will then be available on all controllers, traits and views.
I have module created in the basic project of yii2 and now i want to access or use that module another project/application of mine....
How can I achieve this.
please help me out here.
To use module in different apps there are 3 things you need.
The module must not be dependent on classes from core project. For any class that needs to be implemented by core project the module should define interface and depend on that interface instead of class itself.
The module should use different namespace than app and autoloader must know how to load classes from that namespace. (more about that later)
You have to add module in your config in same way you've added it in first project.
The points 1 and 3 are pretty much self-explaining. If are not sure how to add module in config see the yii2 guide.
Now back to the second point. While naive way of copying module over to second project would work it will turn maintaining the module into nightmare because each change would have to be done in each copy of module. So it's better to keep the code of module in one place and make it available for each project. There are multiple ways of doing that.
If you want to, you can turn your module into extension and make it publicly available through packagist as it was suggested by M. Eriksson in comments. After that you would simply add your extension through composer as any other dependency.
Composer also allows you to define and use private repositories if you don't want to publish your module at packagist. See composer documentation for more details.
The most trivial way is to simply put the code into separate folder outside of project. If you do that, you have to make sure that autoloaders in your projects are capable of finding the files locations to load classes. There are two options how to do that. In any case you will want to avoid conflicts with namespaces used by your project, that's why you need to use different namespace.
Let's assume that you've put your module files into folder /path/to/modules/myModule and all classes in your module belongs to namespace modules\myModule. You have to make sure that your webserver can access that folder and that it can run php scripts there.
First option is to use Yii's autoloader. That autoloader uses aliases to look for classes. If you add #modules alias and point it to /path/to/modules folder, the Yii autoloader will try to look for any class from modules\* namespace in /path/to/modules folder. You can add the alias in your config file (web.php, console.php or any other config file you use):
return [
// ...
'aliases' => [
'#modules' => '/path/to/modules',
// ... other aliases ...
],
];
The second option is to use project's composer.json file to set autoloader generated by composer to load your classes.
{
"autoload": {
"psr-4": {
"modules\\": "/path/to/modules"
}
}
}
You can find more info about this in composer's documentation.
Don't forget to run composer dump-autoload after you change autoload settings in your composer.json file to update the generated autoloader.
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!
I'm building a PHP package and in it I have a folder (let's say /vendor/myvendor/packagename/src/Classes) which I want to autoload using PSR-4.
But I would also like to provide an option to copy that folder from its current location to the project root (let's say /packagename/Classes, something along the lines of Laravel's publish command).
So how could I go about autoloading it?
I would like Composer to first see if the folder exists under the app's root, and if it does then autoload that. Else fall back to the default location inside /vendor. Is that possible?
FWIW, this is Laravel specific package, which means that I could use Laravel's publish command to copy the entire folder where ever, but then
I would have to manually add the new location to autoload;
Even if I do, there would be namespace conflict between the old and new location.
I would like Composer to first see if the folder exists under the app's root, and if it does then autoload that. Else fall back to the default location inside /vendor. Is that possible?
Yes, it is possible.
You need to mention everything on your ServiceProvider for your package.
There are every informations about how to publish your views, assets in the docs and it fits your needs.
Have a look at the docs:
https://laravel.com/docs/5.3/packages#public-assets
I am copying some example for you over here:
If you want to publish your translations, you need to perform this task:
/**
* Perform post-registration booting of services.
*
* #return void
*/
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'courier');
$this->publishes([
__DIR__.'/path/to/translations' => resource_path('lang/vendor/courier'),
]);
}
If you don't have translation in app's lang/vendor/courier then it falls back to your package translation.
You don't need to copy your class codes, which could remain in your package directory. The only things you copy will be your views, assets, translation files etc.
This should help you.
So I'm creating a semi-popular open source project and am looking for ways for its users to customize their copy.
Basically I've zero experience using Composer and next to none with git submodules. I have this file structure pushed to git:
/ROOT
----/subdirectory/
---------/another.file.php
----/main.class.php
----/config.default.php
It would be ideal for users to be able to copy config.default.php to the same directory, rename it to config.php (and by doing that override the default configuration values) - and I'd like them to be able to add their own files to the /subdirectory/ too, allowing them to extend the tool to their unique requirements.
My question is, don't those files get trimmed when pull (in the case of git submodule) or Composer update is performed? If so, how do I achieve the requirements with as little fuss for the end user as possible:
A single, optional, freely editable configuration file
Two directories that can contain files that users create
Above alterations stay within the users own version control system and are not removed when a new version is fetched.
Thank you for your patience in advance.
What most application frameworks/CMSs are doing these days is to split this process in two packages:
one library package (e.g. symfony/symfony, laravel/framework) that contains the bulk of the code, and that will be installed in vendor/
one "bootstrap" package (e.g. symfony/framework-standard-edition, laravel/laravel) that contains an application shell that your users can start from. That would contain default config and a composer.json that includes the library package + whatever else is needed in the "require" section. This bootstrap package is typically not updated via composer since it becomes the application/site of your users. Therefore it should contain as little code as possible, and be essentially only configuration.
You currently load your user config when your class loads:
if ( is_readable( KINT_DIR . 'config.php' ) ) {
require KINT_DIR . 'config.php';
}
If you added a public setConfig() property, then the config could be located anywhere, and your default would still work (code off the top of my wee head):
Public static function setConfig($config) {
if ( is_readable( $config ) ) {
require $config;
}
}