Install the Bundle:
composer require alpixel/jirabundle
(Note: This package is designed for Symfony 2.x, so ensure your project is compatible.)
Register the Bundle in app/AppKernel.php:
public function registerBundles()
{
$bundles = [
// ...
new Alpixel\Bundle\JiraBundle\AlpixelJiraBundle(),
];
}
Configure the Bundle in app/config/config.yml:
alpixel_jira:
base_url: 'https://your-domain.atlassian.net/rest/api/2/' # Replace with your Jira URL
auth:
method:
basic:
username: 'your-email@example.com' # Jira email
password: 'your-api-token' # Use an API token, not password
(Generate an API token in Jira: Profile → Security → API Tokens.)
First Use Case: Fetch a simple endpoint (e.g., user info) in a controller:
use Symfony\Component\HttpFoundation\Response;
class JiraController extends Controller
{
public function userInfoAction()
{
$jira = $this->get('alpixel_jira.api');
$response = $jira->get('/myprefix/currentuser');
$data = $response->getData();
return new Response(json_encode($data));
}
}
Basic API Calls:
Use the service alpixel_jira.api for HTTP requests:
$jira = $this->get('alpixel_jira.api');
$response = $jira->get('/issue/{issueKey}'); // GET request
$response = $jira->post('/issue', ['fields' => [...]]); // POST request
JQL Searches: Query issues with Jira Query Language (JQL):
$response = $jira->search(
'(project = "PROJ" AND status = "Open")',
['maxResults' => 50, 'fields' => ['summary', 'assignee']]
);
$issues = $response->getData()['issues'];
Response Handling:
The Response object provides methods like:
getData(): Raw decoded response.getStatusCode(): HTTP status code.getHeaders(): Response headers.Error Handling: Wrap calls in try-catch for API errors:
try {
$response = $jira->get('/invalid-endpoint');
} catch (\RuntimeException $e) {
// Handle Jira API errors (e.g., 404, 403)
$this->addFlash('error', $e->getMessage());
}
Dependency Injection: Inject the service directly into controllers/services:
use Alpixel\Bundle\JiraBundle\Service\JiraApi;
class MyService
{
private $jira;
public function __construct(JiraApi $jira)
{
$this->jira = $jira;
}
}
Configuration Overrides:
Override config per environment (e.g., config_dev.yml):
alpixel_jira:
base_url: '%env(JIRA_BASE_URL)%'
Rate Limiting: Jira has rate limits (e.g., 5000 requests/3 hours). Cache responses aggressively:
$cache = $this->get('cache.app');
$cacheKey = 'jira_issues_' . md5($jql);
if (!$issues = $cache->get($cacheKey)) {
$issues = $jira->search($jql)->getData();
$cache->set($cacheKey, $issues, 3600); // Cache for 1 hour
}
Authentication:
Endpoint URLs:
base_url must include /rest/api/2/ (e.g., https://example.atlassian.net/rest/api/2/). Missing this will cause 404 errors./myprefix/ for Cloud instances (replace myprefix with your Jira prefix).Response Parsing:
getData() method returns a decoded JSON array. Nested data (e.g., issues.fields) may require additional parsing:
$summary = $issues['issues'][0]['fields']['summary'];
Pagination:
startAt and maxResults to navigate:
$response = $jira->search($jql, ['startAt' => 50, 'maxResults' => 50]);
Symfony 2.x Legacy:
atlassian/jira-php for modern projects.Enable Debug Mode:
Add this to config.yml to log raw responses:
alpixel_jira:
debug: true
Check HTTP Status Codes:
Inspect $response->getStatusCode() for issues (e.g., 403 = auth failure, 400 = invalid JQL).
Validate JQL:
Test queries in Jira’s UI first (e.g., Issues → Search for Issues). Common mistakes:
"PROJ" vs PROJ)."Open" vs "open").CORS Issues: If calling from a frontend, ensure your Jira instance allows CORS or use a backend proxy.
Custom Endpoints:
Extend the JiraApi service by overriding the bundle’s compiler pass:
# config.yml
services:
app.jira.api:
class: App\Service\CustomJiraApi
parent: alpixel_jira.api
calls:
- [setCustomEndpoint, ['/mycustom']]
Add OAuth Support:
Fork the bundle and modify Alpixel\Bundle\JiraBundle\Service\JiraApi to support OAuth2 using league/oauth2-client.
Event Listeners:
Listen for Jira webhooks by extending the bundle or using a separate service to validate signatures (e.g., atlassian-connect events).
How can I help you explore Laravel packages today?