Symfony Tutorial – How to Create a Service in Symfony 2.8
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.
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.
1
2
3
4
5
6
7
8
|
src/
--AppBundle/
----Controller/
--------GeoController.php
--------DashboardController.php
----Resources/
--------config/
------------services.yml
|
GeoController
contains two function and constructor has injected request and container. You can declare as more functions as you want in this controller.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class GeoController extends Controller
{
protected $container;
protected $request;
public function __construct($container) {
$this->container = $container;
$this->request = $container->get('request');
}
public function firstMethod($params){
// Do your magic
}
public function getIpAddress() {
if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '127.0.0.1'){
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] != '127.0.0.1'){
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '127.0.0.1'){
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_X_FORWARDED']) && $_SERVER['HTTP_X_FORWARDED'] != '127.0.0.1'){
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} else if (isset($_SERVER['HTTP_FORWARDED_FOR']) && $_SERVER['HTTP_FORWARDED_FOR'] != '127.0.0.1'){
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_FORWARDED']) && $_SERVER['HTTP_FORWARDED'] != '127.0.0.1'){
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} else {
$ipaddress = $_SERVER['REMOTE_ADDR'];
}
return $ipaddress;
}
}
|
After creating methods, Now it’s time to Register your service. Open services.yml
and add new service as mentioned below.
1
2
3
4
5
6
7
8
|
# src\AppBundle\Resources\config\services.yml
parameters:
# app.example.class: AppBundle\Example
services:
app.geo.access:
class: AppBundle\Controller\GeoController
arguments: ['@service_container']
|
After creating service, finally clear the cache by running below command in root of your application.
1
|
php app/console cache:clear
|
Now, A service is created successfully and accessible from all the Commands, Controller, Bundles.
Inject service and calling getIpAddress()
function is quite easy and fast.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DashboardController extends Controller
{
public function indexAction($params){
// Inject Service
$geo = $this->get('app.geo.access');
// Do your magic
$ipAddress = $geo->getIpAddress();
echo $ipAddress;
}
}
|
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