- What is Laravel?
- Upgrade Laravel To 8.X
- Role Management
- Newsletter Package
- Laravel Vs WordPress CMS
- New Laravel 6.X
- How To Make API In Laravel
- Laravel RSS/XML Feed
- How To Make Theme In Laravel?
- Laravel Project Testing Using PHPunit
- Laravel Package
- Make Contact Form In Laravel
- Laravel Homestead
- Laravel Cron Job
- What is Composer
- How to create an Admin-Panel in Laravel
- Admin Panel in Laravel
- Installation of Laravel in Windows
- Laravel Development
- Benefits of Laravel
- Laravel Notification
- How to make a Blog in Laravel
- Laravel Authentication
Creating a Package:
In this article, I have created a Newsletter package which will manage the newsletter in Laravel. So just follow the steps:
1. First, install fresh Laravel Project:
Run command in your terminal: Composer create-project –prefer-dist laravel/laravel PkgNewsletter “5.8.*”
2. Create a package folder in the root directory and under this create a subfolder newsletter, like this:
3. Next, for package, we have needed two main files: one composer.json and second is Service Provider file.
3.1. Creating composer.json file: Select package path in your terminal and run the composer init command which will create the composer.json file in your newsletter package but after running this command, this will ask various question like package name and vendor, description and author etc… So fill this information.
After completion of init command, you can see composer.json file is created under your package directory. So, open package/newsletter/composer.json file:
3.2. Creating Service Provider: After setup the composer.json file, then we have needed a Service Provider for your package, so create a “src” folder under package/newsletter/, like this:
Create NewsletterServiceProvider.php file under this package/newsletter/src/ directory and add code under boot() and register() function for load, publishing and registering the services of packages file.
4. Modify the composer.json file:
4.1. Open your package composer.json file and add require field, autoload psr-4 and package discovery in your package/newsletter/composer.json file, like this:
Open main composer.json file and add your package path in autoload-dev. Because this will tell your Laravel project that there is a package inside the Laravel project.
4.3. Open your terminal, select the project path and run the composer command: composer dump-autoload. This command will help to recognize the package.
5. Create files for newsletter package: All required files for packages will be created under the src folder. You can visit the GitHub directory for this source code: https://github.com/digitalcrm/pkg-newsletter/tree/master/src.
For this package, I have used the Mailchimp for mail services. To know more about Mailchimp visit https://mailchimp.com/. So, I have configured MailChimp in our package, like this;
Note: When using this newsletter package, you should have to integrate Mailchimp (MAILCHIMP_APIKEY and MAILCHIMP_LIST_ID) in your .env file. For getting the API key and List Id, you should have to create an account in Mailchimp first, where you will get your API Key and List Id.
Upload in packagist.org
Here with few steps, you can upload your package in packagist.org. Just follow the steps:
First you need to open packagist.org and create your account using GitHub and sign in:
After “sign in” in Packagist.org, then you have to upload your package in GitHub repository.
After uploading the package in GitHub, copy the link address and go to the packagist.org then click on submit and paste the address link:
Finally, after submitting package, your package has been uploaded and you will get your composer require command for this package.
How To Use digitalcrm/newsletter Package:
Here, I will discuss how to use this newsletter package. Just follow the steps given below:
First download the Laravel project. Run this composer command:
composer create-project –prefer-dist laravel/laravel NewsLetterPackage
After downloading the Laravel project, next run this composer command:
composer require digitalcrm/newsletter. This command will download the newsletter package in your Laravel project.
Publishing the configuration file run:
php artisan vendor:publish –provider=“Digitalcrm\Newsletter\NewsletterServiceProvider”
This command will publish the config file to config/newsletter.php.
Next, we need view, controller and routes for running the newsletter package. So let’s create a view, controller and routes for this newsletter package.
View file: Create view file under resources/views/subscriber.blade.php and add code:
Controller: For creating controller run the command:
php artisan make:controller NewsLetterController –r.
This command will create a resource controller under app/Http/Controllers/NewsLetterController.php. Then open this file and add code:
Finally create routes under routes/web.php.
Open .env file and integrate MailChimp API_KEY and LIST_ID. You get your own MailChimp API key and List ID from mailchimp.com.
Navigate the URL: http://127.0.0.1:8000/subs in your browser.
• Newsletter page:
• After Submitting
• Open your mail account and you will get subscription page, like this