Symfony routing 500 error - php

It is the first time will use symfony. The setup went ok but now I have a problem with de routing... I have a controller called "GenusController" and use "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;".
My controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\BrowserKit\Response;
class GenusController
{
/**
* #Route("/genus")
*/
public function showAction(){
return new Response("Text");
}
}
When I tried to go to visite the url (http://localhost/aqua_note/web/genus) I get an 500 Internal Server Error. Is there some one can help me with this problem?
I am following this tutorial https://knpuniversity.com/screencast/symfony3/first-page#play

use:
use Symfony\Component\HttpFoundation\Response;
instead of
use Symfony\Component\BrowserKit\Response;

Related

Required "token" not supplied in config and could not find fallback environment variable "TELEGRAM_BOT_TOKEN"

I am using PHP 7.4.1 and Laravel Framework 6.20.16.
I am trying to implement the following library: telegram-bot-sdk and the following version "irazasyed/telegram-bot-sdk": "^2.0",
After installing the sdk and getting my private token from telegram's #botfather. I am trying to use the sdk.
I created a route and a controller:
route
Route::get('telegramHello', 'TelegramController#getHello');
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\Api as Telegram;
use App\Helpers\TelegramResponse as Response;
class TelegramController extends Controller
{
public function getHello() {
$api = new Telegram(); // ----> HERE I GET THE ERROR
$response = $api->getMe();
return Response::handleResponse($response);
}
//...
When opening my route I get the following exception:
The thing I do not understand is that I have created the config telegram.php and loading my correct token from my .env file:
In my .env file it looks like the following:
Any suggestions what I am doing wrong?
I appreciate your replies!
Use Facade, not original API class. Your config is correct, you just using wrong class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\Laravel\Facades\Telegram;
use App\Helpers\TelegramResponse as Response;
class TelegramController extends Controller
{
public function getHello() {
$response = Telegram::getMe();
return Response::handleResponse($response);
}
//...
Also i may recommend you using westacks/telebot instead of irazasyed/telegram-bot-sdk. I created it as irazasyed's was poorly documented and really buggy at a lot of places.
The two comments above helped me the most:
Use "" for your TELEGRAM_BOT_TOKEN
Instead of using your own named .env variable use TELEGRAM_BOT_TOKEN
I hope this works also for others that have this problem.

Symfony 4 and PhpSpreadsheet

Currently I try to use this PHP Spreadsheet Bundle for Symfony 4:
https://github.com/roromix/SpreadsheetBundle
But my attempts to use this bundle are not successful.
Can't find it as a service (php bin/console debug:autowiring)
and the way below is also wrong.
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Roromix\Bundle\SpreadsheetBundle\RoromixSpreadsheetBundle;
class TranslationController extends Controller
{
/**
* #Route("/")
*/
public function index()
{
$RoromixSpreadsheetBundle = new RoromixSpreadsheetBundle();
//...
}
}
?>
I think for get service in your controller :
dump($this->get('phpspreadsheet'));
Because service name as phpspreadsheet : https://github.com/roromix/SpreadsheetBundle/blob/master/src/Resources/config/services.yml

Routing annotation with Symfony4 doesn't match cause of DNS?

I'm trying to make the first page creating tutorial
https://symfony.com/doc/current/page_creation.html
So i use the annotation in my src/Controller/LuckyController.php
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function number()
{
$number = mt_rand(0, 100);
return $this->render('lucky.html.twig', array(
'number' => $number,
));
}
}
And when i try to go in this url
(i.e)OVHServer.fr/project/public/lucky/number
I got a 404 Not Found Error
I guess that it's because I didn't put my DNS in the the project/public directory but I want to know if you know a way for let the Url like that and get the road in my controller.
Thank's
DNS is not relevant. The routing takes effect after the configuration of the server.
The public directory of symfony 2 and 3 is named web, it's named public since symfony4
For example :
OVHServer.fr/app.php/lucky/number
OVHServer.fr/web/app.php/lucky/number
OVHServer.fr/project/public/app.php/lucky/number
When your server is configured to hide the app.php it would give you
OVHServer.fr/lucky/number
OVHServer.fr/web/lucky/number
OVHServer.fr/project/public/lucky/number

Creating the first controller in Symfony

I have just started to learn Symfony. The fact is that I do things and they work but I don't understand what I am doing and that must be the wrong way starting to learn something.
Here is a simple controller:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
/**
* #Route("/hello")
*/
public function helloAction() {
return $this->render('', array());
}
}
So in this case when I type
/**
* #Route("/hello")
*/
my editor automatically adds this line of code:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
What is that doing?
I have learned that namespaces must match the directory path but there is no way I could find that path in my Symfony project. So is it outside of my project? If so, this will be good to know in case I want to upload my project on the web server.
I would appreciate if someone could please explain this "mystery".
Your IDE just added the dependency to Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;, which is required to use the #Route annotation.
It is placed under the vendor/ directory, which of course you will need to upload along with your sources.

Symfony2 - Creating a URL

I've just started using Symfony. I want to echo the $bookid when I call a URL like book/5 , but I stuck somewhere.
Here's my DefaultController.php file
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller {
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction() {
return $this->render('default/index2.html.php');
}
}
file: /Myproject/app/Resources/views/default/index2.html.php
<?php
echo $id;
?>
When I call the book/6 , I get a blank page. What's missing? Do I need to change somewhere else, too?
You should declare that variable in your action and pass it to your view.
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction($id)
{
return $this->render('default/index2.html.php', array(
'id' => $id
));
}
}
Whenever you have a parameter defined in your URL, you also need to "declare" it in your action function, so symfony maps it. Then, if you want to use it in your view, you have to pass it along.
If you are just starting with Symfony, I strongly recommend reading the Symfony Book and Cookbook. They are full of examples and relatively easy to understand, even for a newbie.
Other than that, the answer from smottt is correct. You add {id} in your route definition and receive it as parameter in your controller action.
Symfony book: Routing

Categories