When developing an Application/Website in Core PHP We always need a common PHP file which includes Common PHP function. Before calling Common PHP function you always need to inject the common PHP file using require_once PHP function in our code base. Symfony framework provides Service functionality which makes programming easier, fast and promotes reusable and decoupled code.

Create service in symfony

All you need to do is creating Simple PHP class and inject it by creating a service and you can use it from anywhere in your application.

Create a Service in Symfony Framework

After install Symfony Application, First of all we need to create service class in which we will declare variables and global methods.

Directory Structure

GeoController.php

GeoController contains two function and constructor has injected request and container. You can declare as more functions as you want in this controller.





services.yml

After creating methods, Now it’s time to Register your service. Open services.yml and add new service as mentioned below.

After creating service, finally clear the cache by running below command in root of your application.

Now, A service is created successfully and accessible from all the Commands, Controller, Bundles.

DashboardController.php

Inject service and calling getIpAddress() function is quite easy and fast.

 

Read More:

Composer – Dependency manager for PHP

Create a RESTful Web Service API with Slim

How To Integrate Stripe Payment Gateway Using PHP and JavaScript

sFileTypeChecker – JQuery Plugin To Validate Input File

sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">