PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] Minimal symfony setup with annonations

 January 31, 2022     annotations, symfony     No comments   

Issue

i want to create a simple api based on Symfony framework.

The controllers are already implemented.

1) What is the minimal setup of composer packages i need?

2) How can i setup a working index.php file that creates the routes from the annotations, match the urls and outputs the response?

Thank you very much!

Here is an example code of one of my controllers located in src/Bitter/Cloud/Server/Controller/PhotosController.php:

<?php

namespace Bitter\Cloud\Server\Controller;

use Bitter\Cloud\Cloud;
use Bitter\Cloud\Services\PhotosService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class PhotosController extends AbstractController
{
    protected Request $request;
    protected PhotosService $service;

    public function __construct()
    {
        $this->request = Request::createFromGlobals();
        $this->service = Cloud::getServices()->getPhotos();
    }

    /**
     * Send a request to fetch photos and videos from the photo service.
     *
     * @Route("/photos/get_photos")
     *
     * @link project://docs/services/photos/get-photos.md
     */
    public function getPhotos(): JsonResponse
    {
        $this->service->init();

        return new JsonResponse([
            "success" => true,
            "photos" => $this->service->getPhotos()
        ]);
    }
}

All other controllers are similar.

Here is my composer.json setup:

{
  "name": "bitter/cloud-server",
  "description": "Cloud Server for PHP.",
  "license": "MIT",
  "type": "project",
  "homepage": "*********removed*********",
  "authors": [
    {
      "name": "*********removed*********",
      "email": "*********removed*********",
      "role": "Developer"
    }
  ],
  "minimum-stability": "dev",
  "repositories": [
    {
      "name": "bitter/cloud-api",
      "type": "vcs",
      "url": "git@bitbucket.org:*********removed*********"
    }
  ],
  "keywords": [
    "cloud",
    "php",
    "api"
  ],
  "support": {
    "issues": "*********removed*********"
  },
  "require": {
    "bitter/cloud-api": "*",
    "symfony/http-foundation": "5.0.0",
    "symfony/routing": "5.0.0",
    "symfony/config": "5.0.0",
    "doctrine/annotations": "1.8.0",
    "symfony/framework-bundle": "5.0.0",
    "doctrine/cache": "1.8.0"
  },
  "autoload": {
    "psr-4": {
      "Bitter\\Cloud\\Server\\": "src/Bitter/Cloud/Server"
    }
  }
}


Solution

All you need to do is create a simple application with composer:

composer create-project symfony/skeleton

then add an annotations package:

composer require annotations



Answered By - Vladimir Tarkhanov
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing