How to run package in laravel 5?
I have downloaded one chat package from INTERNET. And, I have run command vendor:publish and it successfully published.
Now I don't know how to run that.
First you need to check whether the package ships with any service provider and facade. If so, add them in your config/app.php file. Package's service provider should go under providers array and facade should go under aliases array.
As soon as you specify providers under providers array, Laravel now know that you have called an external package, so it will load them. Thus, the package now will be available for your usage.
The easy way to run a package in laravel is:
add the package to composer.json file
then in terminal write composer dump-autoload
Related
I've set up a fresh installation of Laravel in Homestead and I've installed PhpRedis as recommended in the Laravel docs https://laravel.com/docs/7.x/redis#phpredis.
I followed this guide for installing PhpRedis https://webstoked.com/install-phpredis-laravel-ubuntu/
In both the Laravel docs, and the guide I've linked for installing PhpRedis, I'm instructed to rename the Redis alias in config/app.php.
If you plan to use PhpRedis extension along with the Redis Facade alias, you should rename it to something else, like RedisManager, to avoid a collision with the Redis class. You can do that in the aliases section of your app.php config file.
- Laravel Docs
To further add to my confusion, the Laravel docs then go on to say that you should remove the alias entirely.
To avoid class naming collisions with the Redis PHP extension itself, you will need to delete or rename the Illuminate\Support\Facades\Redis facade alias from your app configuration file's aliases array. Generally, you should remove this alias entirely and only reference the facade by its fully qualified class name while using the Redis PHP extension.
- Laravel Docs
My main questions are:
What does, "If you plan to use PhpRedis extension along with the Redis Facade alias", mean?
When should I rename the alias, remove it, or leave it as-is?
Depending on if I rename or remove the alias, how will this affect using Redis?
There are two different configurations/ways to use redis in a laravel project.
First one is to use predis and it is in your vendor folder. This one is "Flexible and feature-complete Redis client for PHP and HHVM" located here. It is a package/library written in php.
The other way to do is using PhpRedis, it is an extension written in C and located here.
protected function connector()
{
switch ($this->driver) {
case 'predis':
return new Connectors\PredisConnector;
case 'phpredis':
return new Connectors\PhpRedisConnector;
}
}
What does, "If you plan to use PhpRedis extension along with the Redis Facade alias", mean?
In the framework there is a check. While creating the PhpRedis client of Redis, it is checking whether the new Redis instance is Facade because PhpRedis is also using Redis name is you can see from here. So if you want to use PhpRedis in your laravel framework you better rename your facade because it will cause collision.
When should I rename the alias, remove it, or leave it as-is?
If you are going to use predis as client, then you can leave it as-is. If you are going to use PhpRedis as client, then you need to rename alias.
Depending on if I rename or remove the alias, how will this affect using Redis?
You will use RedisManager::someMethod() if you choose PhpRedis. You will use Redis::someMethod() if you use predis.
Please point out any naivete or incorrect assumptions I'm making about Laravel, Composer, PHPUnit, etc.
I had a class called SpeechToTextHelper that was inside a Laravel project, and it used facades like this:
use Cache;
use Log;
use Storage;
Then, since I wanted to share it between multiple Laravel projects, I moved it into a separate repo and required it (into the first project) as a dependency via Composer.
The code all seems to run fine.
My question is different from Using Laravel Facades outside Laravel
What I want to know is:
Now that I also want to write PHPUnit tests for SpeechToTextHelper in my new tools repo, I see errors like RuntimeException: A facade root has not been set. and Error: Class 'Log' not found, presumably because this tools repo has no awareness of Laravel. I guess this means my production code has been working just by side-effect.
In my new tools repo (where my SpeechToTextHelper now is), how am I supposed to indicate (maybe somewhere in composer.json?) that the code will only work if Laravel's facades exist and are initiated properly?
How can I fix my separate repo's code so that its tests can run and also so that it ensures that it can only be "required" by a Laravel project?
P.S. https://laravel.com/docs/5.7/facades says "When building a third-party package that interacts with Laravel, it's better to inject Laravel contracts (https://laravel.com/docs/5.7/contracts) [which live in their own GitHub repository] instead of using facades." "If you are building a package, you should strongly consider using contracts since they will be easier to test in a package context."
But I do not see contracts for Log or Storage at all.
I think you are looking for Laravel component repositories
Cache - This component shows how to use Laravel's Cache features in non-Laravel applications.
Log - This component shows how to use Laravel's Log features in non-Laravel applications.
This video shows, how you can use eloquent outside laravel, I think that will give you better idea.
I'm not positive that this is the best approach, so I'd love if others
would provide better answers.
For production code
My composer.json still has this in the "require" section: "laravel/framework": "5.7.*",.
I plan to only ever require this tools library from within a Laravel app. I'm not sure that this is the right way to make that a rule, but my production code at least seems to be working.
For tests
As for tests, what seems to have been necessary was to add these files from https://github.com/laravel/laravel/tree/2a1f3761e89df690190e9f50a6b4ac5ebb8b35a3:
app/Console/Kernel.php
app/Providers/AppServiceProvider.php
app/Providers/AuthServiceProvider.php
app/Providers/EventServiceProvider.php
app/Providers/RouteServiceProvider.php
bootstrap/cache/.gitignore
bootstrap/app.php
bootstrap/autoload.php
config/app.php
config/database.php
config/logging.php
config/view.php
storage/logs/laravel.log
tests/CreatesApplication.php
tests/TestCase.php
Perhaps those are the minimum set of barebones Laravel files without which tests can't run.
Then I made sure that each test class extended tests/TestCase.php. And I adjusted the namespaces.
So i integrated this package to my application, https://github.com/thekordy/ticketit and this package has its own view and i want to modify the views like the create.blade.php,.. how would i do this appropriately?
because my current solutions is just to copy the view from the package change the return view('create'); in my controller?
You will notice many packages include in its instalation proccess this command:
php artisan vendor:publish
What it does behind the scenes is it looks for all package's service providers instructions in order to figure out it anything should be "published" (meaning copying from vendor folder to config/, views/ etc)
I looked at your package's service provider:
https://github.com/thekordy/ticketit/blob/0.2/src/TicketitServiceProvider.php and from line 179 to 182, the package seems to have the correct "publish" instructions.
Which means probably that the documentation skiped this part.
So, you should just basically hit the command php artisan vendor:publish and it will copy views, translations, public and migrations folder to your own apps folders.
Then you will see inside your resources/views a vendor folder, which will now have the ticketit views inside it.
Laravel figure it out when you say "view('ticketit.form.index')" and it will first look inside your own resources folder, if it don't find the content, it will try to look inside the package's folder.
For more, read the docs:https://laravel.com/docs/5.4/packages#views
Just to add one more thing, you can choose which type of resources to publish by using the tags for the publish command
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="views"
Publish only ticketit views (destination: base_path/resources/views/vendor/ticketit)
If for any reason, found extending views is not enough and want to extend functionalities or controllers themselves, ticketit allows using of custom routes file, you can use that to point to your own custom controllers.
Other supported vendor publish tags:
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="lang"
Publish only ticketit translation files (destination: base_path/resources/lang/vendor/ticketit)
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="db"
Publish only ticketit migration files (destination: base_path/database/migrations)
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="public"
Publish only ticketit web resources (js, css, ..) files (destination: public_path/vendor/ticketit)
For anyone facing this issue the solution is Laravel default convention, as shown in documentation: https://laravel.com/docs/5.4/packages#views (publishing views topic)
In short, you need to use the resource path as:
'__DIR__.'/path/to/views' => resource_path('views/vendor/view_namespace)'
where view_namespace is the second parameter inside your loadViewsFrom method.
So given the file: https://github.com/thekordy/ticketit/blob/0.2/src/TicketitServiceProvider.php
If line 102 is $this->loadViewsFrom($viewsDirectory, 'ticketit');
Line 103 should be:
$this->publishes([$viewsDirectory => base_path('views/vendor/ticketit')], 'views');
How do I split an app into different modules/packages?
At the moment I plan the following packages:
Common (models, services, ... used in other packages)
Web (Frontend for users)
API
You can split your laravel app using a specific package called Lpackager.
Easy to use.
Generate your package via an artisan command php artisan lpackager:package <PackageName> <PackagePath> <"NameSpace">
Ability to generate CRUD for your packages/modules using scaffold-interface.
In Laravel 4.2 is it possible to create a package which automatically registers the ServiceProvider without the user adding the path manually to the app.php file?
This way one could just run composer update after adding a package to the composer.json file and would be ready to go.
I think that there is no way to register your main service provider than putting it in the app.php file. In fact you can, but it will always require something from the developer. You can't get away with it with a simple composer update.
You can register sub service providers with App::register('MyApp\Providers\MyServiceProvider'); inside your package.
No, there is no way for a package to register itself.