Symfony Routing Component not routing url - php

I have mvc php cms like this folder structure:
application
---admin
--------controller
--------model
--------view
--------language
---catalog
--------controller
------------------IndexController.php
--------model
--------view
--------language
core
--------controller.php
//...more
public
--------index.php
vendor
I install symfony/router component for help my route url using composer json:
{
"autoload": {
"psr-4": {"App\\": "application/"}
},
"require-dev":{
"symfony/routing" : "*"
}
}
Now with route documents I add this code for routing in index.php:
require '../vendor/autoload.php';
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$route = new Route('/index', array('_controller' => 'App\Catalog\Controller\IndexController\index'));
$routes = new RouteCollection();
$routes->add('route_name', $route);
$context = new RequestContext('/');
$matcher = new UrlMatcher($routes, $context);
$parameters = $matcher->match('/index');
In My IndexController I have :
namespace App\Catalog\Controller;
class IndexController {
public function __construct()
{
echo 'Construct';
}
public function index(){
echo'Im here';
}
}
Now in Action I work in this url: localhost:8888/mvc/index and can't see result : Im here IndexController.
How do symfony routing url work and find controller in my mvc structure? thank for any practice And Help.

The request context should be populated with the actual URI that's hitting the application. Instead of trying to do this yourself you can use the HTTP Foundation package from symfony to populate this:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
$context = new RequestContext();
$context->fromRequest(Request::createFromGlobals());
It's also documented here: https://symfony.com/doc/current/components/routing.html#components-routing-http-foundation
After the matching ($parameters = $matcher->match('/index');) you can use the _controller key of the parameters to instantiate the controller and dispatch the action. My suggestion would be to replace the last \ with a different symbol for easy splitting, like App\Controller\Something::index.
You can then do the following:
list($controllerClassName, $action) = explode($parameters['_controller']);
$controller = new $controllerClassName();
$controller->{$action}();
Which should echo the response you have in your controller class.

Related

Can't load class on Slim 3

I'm trying to load my custom classes for the model on Slim 3 (using the skeleton) so I made this:
In app/composer.json:
"autoload": {
"psr-4": {
"App\\Classes\\": "/src/classes"
}
},
In routes.php I have this setting:
<?php
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Container;
// Routes
$app->get('/sugiere', function (Request $request, Response $response, array $args) {
// Sample log message
$this->logger->info("Slim-Skeleton '/' route");
$cat_mapper = new \App\Classes\CategoryMapper($this->db);
$comuna_mapper = new \App\Classes\ComunaMapper($this->db);
$lang_mapper = new \App\Classes\LanguageMapper($this->db);
$netw_mapper = new \App\Classes\NetworkMapper($this->db);
$com_list = $com_mapper->getComunaList();
$cat_list = $cat_mapper->getCategoryList();
$lang_list = $lang_mapper->getLangList();
$netw_list = $netw_mapper->getNetworkList();
By the way I added to all classes a namespace App\Classes on top.
Your path /src/classes looks incorrect. It's unlikely your src directory is in the filesystem root.
Change your composer.json file to
"autoload": {
"psr-4": {
"App\\Classes\\": "src/classes/"
}
}
and run
composer dump-autoload
to re-generate the autoload.php file.
See https://getcomposer.org/doc/01-basic-usage.md#autoloading

Symfony3 Hook Up Annotation Routes

I'm writing my own PHP framework built on top of Symfony components as a learning exercise. I followed the tutorial found at http://symfony.com/doc/current/create_framework/index.html to create my framework.
I'd now like to wire up my routes against my controllers using annotations. I currently have the following code to setup the routing:
// Create the route collection
$routes = new RouteCollection();
$routes->add('home', new Route('/{slug}', [
'slug' => '',
'_controller' => 'Controllers\HomeController::index',
]));
// Create a context using the current request
$context = new RequestContext();
$context->fromRequest($request);
// Create the url matcher
$matcher = new UrlMatcher($routes, $context);
// Try to get a matching route for the request
$request->attributes->add($matcher->match($request->getPathInfo()));
I have come across the following class to load the annotations but I'm not sure how to use it:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php
I'd appreciate it if someone could help.
Thanks
I've finally managed to get this working. First I changed where I included the autoload.php file to the following:
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Then I changed the routes collection bit (in the question) to:
$reader = new AnnotationReader();
$locator = new FileLocator();
$annotationLoader = new AnnotatedRouteControllerLoader($reader);
$loader = new AnnotationDirectoryLoader($locator, $annotationLoader);
$routes = $loader->load(__DIR__ . '/../Controllers'); // Path to the app's controllers
Here's the code for the AnnotatedRouteControllerLoader:
class AnnotatedRouteControllerLoader extends AnnotationClassLoader {
protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) {
$route->setDefault('_controller', $class->getName() . '::' . $method->getName());
}
}
This has been taken from https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/Routing/AnnotatedRouteControllerLoader.php. You may wish to modify it to support additional annotations.
I hope this helps.

Unable to find controller in Silex Application

Good morning,
I have been developing an application using Silex for the past couple of weeks and last night I either made a change to my code or something was updated as part of updating composer but it will not work.
I am using the 'Igorw\ConfigServiceProvider' to load up my routes which link to my configured controllers. But when I access the webpage I get the error message:
InvalidArgumentException: Unable to find controller "controllers.admin:index".
My files are as follows
composer.json
{
"require": {
"silex/silex": "1.2.*#dev",
"igorw/config-service-provider": "1.2.*#dev",
"symfony/yaml": "2.5.*#dev"
},
"autoload": {
"psr-4": {
"Turtle\\Controllers\\": "src/turtle/controllers"
}
}
}
config/routes.yml
config.routes:
admin:
pattern: /admin
defaults: { _controller: 'controllers.admin:index' }
method: GET
web/index.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use \Igorw\Silex\ConfigServiceProvider;
use \Turtle\Controllers\AdminController;
$app = new Silex\Application;
$app["debug"] = true;
// load the routes
$app -> register (new ConfigServiceProvider(__DIR__ . "/../config/routes.yml"));
foreach ($app["config.routes"] as $name => $route) {
$app -> match($route["pattern"], $route["defaults"]["_controller"]) -> bind($name) -> method(isset($route["method"]) ? $route["method"] : "GET");
}
// register the classes
$app["controllers.admin"] = $app -> share(function($app) {
return new AdminController($app);
});
$app -> run();
src/turtle/controllers/AdminController.php
<?php
namespace Turtle\Controllers;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
class AdminController {
protected $app;
public function __construct(Application $app) {
$this -> app = $app;
}
public function index (Request $request) {
return "Hello World!";
}
}
I have checked the $app variable and it contains an instantiated AdminController class, but for some reason the system is picking up the controller properly. I really do not understand what has happened and can only put it down to an obscure mistake or an update.
Can anyone shed any light on this please?
Thanks, Russell
I cross posted this on the Silex GitHub issue site at https://github.com/silexphp/Silex/issues/919 and the problem has been pointed out. Kudos to Dave Marshall.
The web/index.php file is not registering the Silex ServerControllerServiceProvider. After adding this in the system now works. The updated file now looks like:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use \Igorw\Silex\ConfigServiceProvider;
use \Turtle\Controllers\AdminController;
$app = new Silex\Application;
$app["debug"] = true;
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
// load the routes
$app -> register (new ConfigServiceProvider(__DIR__ . "/../config/routes.yml"));
foreach ($app["config.routes"] as $name => $route) {
$app -> match($route["pattern"], $route["defaults"]["_controller"]) -> bind($name) -> method(isset($route["method"]) ? $route["method"] : "GET");
}
// register the classes
$app["controllers.admin"] = $app -> share(function($app) {
return new AdminController($app);
});
$app -> run();
I must have removed the line inadvertently when I was re-organising the files.

Silex app->redirect does not match routes

Let my application run on localhost, the path is: localhost/silex/web/index.php, defined routes as in the code below, I'd expect visiting localhost/silex/web/index.php/redirect redirects me to localhost/silex/web/index.php/foo and displays 'foo'. Instead it redirects me to localhost/foo.
I am new to Silex and maybe I got it all wrong. Could someone explain where is the problem? Is it correct behavior and it should redirect for absolute paths? Thanks.
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app['debug'] = true;
$app->get('/foo', function() {
return new Response('foo');
});
$app->get('/redirect', function() use ($app) {
return $app->redirect('/foo');
});
$app->run();
The redirect url expects an url to redirect to, not an in-app route. Try it this way:
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->get('/foo', function() {
return new Response('foo');
})->bind("foo"); // this is the route name
$app->get('/redirect', function() use ($app) {
return $app->redirect($app["url_generator"]->generate("foo"));
});
For internal redirects, which does not change the requested URL, you can also use a sub-request:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
$app->get('/redirect', function() use ($app) {
$subRequest = Request::create('/foo');
return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
});
See also Making sub-Requests.
Up to "silex/silex": ">= 2.0", a native trait allow you to generate an URL based on the route name.
You can replace :
$app['url_generator']->generate('my-route-name');
By :
$app->path('my-route-name');
Then use it to redirect :
$app->redirect($app->path('my-route-name'));
Another possibility is to create a custom trait to directly redirect with a route name :
namespace Acme;
trait RedirectToRouteTrait
{
public function redirectToRoute($routeName, $parameters = [], $status = 302, $headers = [])
{
return $this->redirect($this->path($routeName, $parameters), $status, $headers);
}
}
Add the trait to your application definition :
use Silex\Application as BaseApplication;
class Application extends BaseApplication
{
use Acme\RedirectToRouteTrait;
}
Then use it wherever you need it :
$app->redirectToRoute('my-route-name');

How to bind route with controller with Symfony2 Routing component

I want to use a standalone Symfony2 Routing component in my small site. I've created this according to documentation and some examples:
$request = Request::createFromGlobals();
$routeTest = new Symfony\Component\Routing\Route('/route-test', array('controller' => 'test'));
$routes = new Symfony\Component\Routing\RouteCollection();
$routes->add('test', $routeTest);
$context = new Symfony\Component\Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Symfony\Component\Routing\Matcher\UrlMatcher($routes, $context);
$matcher->match($request->getPathInfo());
I don't understand how I should call my controller test, that I've passed to the Route constructor. As a result I want to get something like Silex Route matching:
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
And sorry for my english...
$matcher->match() returns[1] the attributes of the matching route[2] (including a special _route attribute containing the route name [3]).
The controller default is included in the attributes too, so you can easily access it and then use something like call_user_func to call the controller:
// ...
$attributes = $match->match($request->getPathInfo());
$controllerResult = call_user_func($attributes['controller']);

Categories