cant run the controller with symfony 2 - symfony

I generated my bundle with netbeans with no error , then went to my main config file in the app directory and set this:
first_app:
resource: "#FirstAppBundle/Resources/config/routing.yml"
prefix: /
Then set this in my bundle routing.yml file:
first_app_homepage:
pattern: /hello/{name}
defaults: { _controller: FirstAppBundle:Hello:index }
I have got this controller:
namespace DimaVendor\MytestsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController extends Controller {
public function indexAction($name) {
return new Response('<html><body>' . $name . '</body></html>');
}
}
I get 404 when I go to there:
http://localhost/app.php/hello/Ryan
why?
I also cleaned my cache and found no error

app.php and app_dev.php scripts are the entry points for Symfony apps, and they lie in web/ subfolder in the project - in this case, point your browser to:
http://localhost/PhpProject1/web/app_dev.php/hello/Ryan
.htaccess comes in web/ folder, and redirects any requests to:
http://localhost/PhpProject1/web/some/path
to:
http://localhost/PhpProject1/web/app.php/some/path
in your case. Have a look!

Related

How to set host for routes in sonata admin?

How to make sonata always use root domain for url it generates?
I have a site, some of the pages are on the root domain, some are on the subdomain.
I need to have url to edit post that always refers to root domain even if the link is on the subdomain.
Edit
It looks like I could solve the issue for sonata
adding to sonata route options and defaults in routing.yml
admin_area:
resource: "#SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /backoffice
options:
compiler_class: 'MyApp\Router\RouteCompiler'
defaults:
default_host: %router.request_context.host%
_sonata_admin:
resource: .
type: sonata_admin
prefix: /backoffice
options:
compiler_class: 'MyApp\Router\RouteCompiler'
defaults:
default_host: %router.request_context.host%
router.request_context.host is my root domain.
The route compiler class looks like:
class RouteCompiler extends BaseRouteCompiler
{
public static function compile(Route $route)
{
if ($route->getHost() === "") {
$route->setHost($route->getDefaults()['default_host']);
}
return parent::compile($route);
}
}
When I use in twig url function, it generates url to root domain even on
pages from subdomain.

Routing issue with Symfony 3.4

I'm trying to access through localhost (Xampp) the index file from the folder in a Symfony project. Although i'm writing the following path it cannot find the page: http://localhost/google_analytics_app/consolytics-app/analytics/
tree-folder-structure
Following the instructions i use the base file in my index.html.twig file under the folder viwes/analytics/
{% extends 'base.html.twig' %}
and use my own index file under the folder/analytics/index.html.twig.
I also used annotations inside the src/AppBundle/Controller/IndexController.php :
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Library\GoogleAnalytics;
use Doctrine\Common\Util\Debug as dump;
class IndexController extends Controller
{
/**
* #Route("/analytics/index", name="homepage")
*/
public function indexAction(Request $request)
{
....
return $this->render('index.html.twig');
}
}
When type the path it shows 404 error message. Can someone help with this routing issue? In my composer.json file:
"require": {
"twig/twig": "^1.0||^2.0"
},
The problem is that You didn't register your routings in routing.yml file.
You have to understand that annotations are describe routes, but your router component don't now anything about your routes before You register that one in routing.yml file. From this file (v3.4) router gets your routes.
https://symfony.com/doc/3.4/best_practices/controllers.html#routing-configuration
# app/config/routing.yml
app:
resource: '#AppBundle/Controller/'
type: annotation
Pay attantion for type parameter. Annotation says to router that your routes are actually annotations :)
Your param for the render-method is wrong. It has to be return
$this->render('views/analytics/index.html.twig');

symfony3 REST API issue with yml routing using FOSRestBundle

I've finally got my first API in Symfony3 which is actually working.
Very well so far but problems came when I tried using YML configs for routing instead of annotation. It is driving me crazy because it seems to work, in fact when I chance for eg the controller name, it gives me an internal server error but, when everything is "correct", it seems it's unable to find the method in my controller.
So here is my code:
General routing settings
#config/routing.yml
user_routes:
resource: "#AppBundle/Resources/config/user_routes.yml"
type: rest
Bundle routing settings
#AppBundle/Resources/config/user_routes.yml
user:
type: rest
resource: AppBundle\Controller\UserController
And finally my controller:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use AppBundle\Entity\User;
class UserController extends FOSRestController
{
// /**
// * #Rest\Get("/user")
// */
public function getAction()
{
$restresult = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();
if ($restresult === null) {
return new View("there are no users exist", Response::HTTP_NOT_FOUND);
}
return $restresult;
}
}
Using annotation it works well and everybody's happy but when I use these settings I get a 404. I tried adding prefix: /api and name_prefix: api_ according to Symfony official documentation but it didn't work.
I tried also adding a defaults: { _controller: AppBundle:Controller:UserController:get but error 404 was always round the corner.
As I said, if I change the name of the controller class in the user_routes.yml, I get a 500 error so it seems the routing is being read but it's evident that something is missing here and I'm unable to find it neither on the official documentation nor in other places.
Change the name of the action from this:
public function getAction()
to this:
public function getUsersAction()
DOCUMENTATION
Well, just in case it may become useful for someone, I found the solution.
The problem lay in the yml user_routes.yml.
Here is the correct configuration:
path: /user
defaults: { _controller: AppBundle:User:get }
requirements:
_method: GET
That's it ;)

Setting index route in Symfony 2

I've got Symfony2 installed, and a mostly working site, the only issue is I don't know how to set a default route. Currently, I access my index and other routes with the following URLs:
www.example.com/app_dev.php/index
www.example.com/app_dev.php/example_route
I'd like to have www.example.com default to the index route, so I can get the same results with the following URLs:
www.example.com
www.example.com/example_route
I'm running lighttpd as my web server. How can I configure lighttpd/Symfony2 to do this?
Just create a route that maps to the / pattern :
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: AcmeHomeBundle:home:show }
This will route to whatever controller you specify.
Great Docs:
How to Configure a Redirect without a custom Controller
Routing
I used below code to set home page route.It's working fine
Symfony version : Symfony 3.2.8
homepage:
path: /
defaults: { _controller: AppBundle:Home:index}
For me Symfony 4.1.x
Edit the file
# app/config/routes.yaml
index:
path: /
controller: App\Controller\YourIndexController::yourIndexFunction
There's
App\Controller is the namespace you declare at the start of the Controller class, and after is your class name and method name to route to.
I solved this problem by just removing the following from routing_dev.yml
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
That is assuming you have setup a default / route in a routing.yml file or by defining the route in a controller like:
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
return array('name' => '1');
}
The answer given by ManseUK is very much helpful but I have little elaboration
1)
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: AcmeHomeBundle:home:show }
2) rename the app_dev.php to index.php and this will route to the home page automatically

Get routing requirement variable in Symfony2

in order to create a navigation for me webinterface I'd like to get a variable from the routing config of my bundle. I define the available pages in mybundle/Resources/config/routing.yml.
mybundle_homepage:
pattern: /{_locale}/{branch}/{page}
defaults: { _controller: mybundle:mycontroller:index, _locale: de, branch: x.x.x, page: start }
requirements:
_locale: de|en
page: start|functions|events|constants|styleguide
Now I had a look at the Symfony2 YAML Parser and I have to provide a filepath to it's static method parse: http://symfony.com/doc/2.0/reference/YAML.html
mycontroller.php
use Symfony\Component\Yaml\Yaml;
class mycontroller extends Controller
{
public function indexAction($_locale, $branch, $page)
{
$routing = Yaml::parse('../Resources/config/routing.yml');
var_dump($routing);
}
}
I thought I could do it that way because the folder hirarchy looks like that:
mybundle
Controller
mycontroller.php
Rescources
config
routing.yml
But it's not working. Any ideas or maybe another way to get the requirements.page array from the routing file?
Regards, Ben
You should be able to access the router service inside a class that's DI container aware. So, you can write something like:
$routes = $this->container->get('router')->getRouteCollection();
$route = $routes->get('my_route_name');
print_r($route->getRequirements());

Resources