How to Hide parameters in URL in Symfony2 - symfony

I don't want the user see the parameters in my URL.
Example :
my_route:
path: /***/{id}
defaults: { _controller: MyBundle:Default:myaction}
the route will be generated like this :
***/product/1
i want just see the
***/product

As explained in comments, the request needs something to identify the resource you want to fetch. But this has nothing to do with URL rewriting. If you want to display the slug of the resource instead of the id, do the following :
my_route:
path: /products/{slug}
defaults: { _controller: MyBundle:Default:myaction}
Then, in MyBundle/Controller/DefaultController.php :
public function myactionAction(Request $request, $slug)
{
$product = $this->getDoctrine()->getManager()->getRepository('MyBundle:Product')->findOneBy(["slug" => $slug]);
}
and when you want to create a link to this product, use
link to the product

Related

Redirecting a controller and action method to its URL

I have a project that I am working on that was done in ASP.NET MVC 5.
They used the default URL structure that comes standard with the framework. For example, the AGM page was constructed like this:
Controller: Home
Action method: AGM
Without any routing setup, to access this page, you would need to go to the following URL:
www.example.com/Home/AGM
This is the URL that they sent to the press. This URL looks horrible and I want to implement a cleaner URL structure which will look like this:
www.example.com/agm
I have set it up like this in the RouteConfig.cs file:
routes.MapRoute(
name: "AGM",
url: "agm",
defaults: new { controller = "Home", action = "AGM" }
);
What I want to achieve is if the user types in www.example.com/Home/AGM then it needs to display the URL like www.example.com/agm. Instead, it displays like www.example.com/Home/AGM.
I'm not sure how to implement this?
Configure routing and define action in the url
routes.MapRoute(
name: "AGM",
url: "{action}",
defaults: new { controller = "Home", action = "AGM" }
);
Then in the AGM method redirect to www.example.com/agm
if (Request.Path.ToLower() == "/home/agm")
{
return Redirect("/agm");
}
You can simply add [Route("AGM")] and [Route("Home/AGM")] top of your Action method:
[Route("AGM")]
[Route("Home/AGM")]
public IActionResult AGM()
{
return View();
}

symfony2 : apply different template with special parameter?

How can I apply different template (view) when I type ?t=1 in the url ?
Examples :
mysite.com -> opens home with default template, let's say home.twig
mysite.com/?t=1 -> opens home with another template : home1.twig (or 1/home.twig)
Is it possible ?
Thanks :)
Sure, you can define different routes for this, but second url should look like "mysite.com/1":
route_1:
pattern: /
defaults: { _controller: AppBundle:Index:home }
route_2:
pattern: /{t}
defaults: { _controller: AppBundle:Index:custom }
requirements:
t: '[0-9]+'
A second approach is to load different views based by existence of "t" parameter:
public function homeAction(Request $request)
{
$view = $request->query->has('t') && $request->query->get('t') == 1 ? 'home1.twig.html' : 'home.twig.html';
return $this->render($view, $params);
}

Custom views (or routes) in FOSRestController

I am using FOSRest Bundle to implement Rest API's.
I have the function getTermsAction() which returns all the terms from the DB.
I have a template listTerms.html.twig which shows the list of terms.
I want to implement another function listTermsAction() which will render this template and return it.
Something like this (I added this to my controller)
/**
* #Route("/listTerms",name="listTerms")
*/
public function listTermsAction()
{
$view = $this->view(null,200)
->setTemplate("TermsBundle:Default:listTerms.html.twig");
return $this->handleView($view);
}
I tried to this by adding the following code in routing.yml file
list_terms:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTermsAction}
But it doesn't work!
It just shows me "null" in the browser
The Solution i have given works but that snippet should be above the rest route in my routing.yml
That is routing.yml should be like
list_term:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTerms}
terms:
resource: Madhuri\TermsBundle\Controller\TermsController
type: rest
prefix: /
list_term route should be above terms route

Troubleshooting "The controller for URI is not callable" error

I'm working on Symfony 2.3 and I declared a new route and new controller, but when I call this controller from the browser I get this error:
The controller for URI "/user/1" is not callable. in /dev.mydomain.org/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php at line 82
This is my simple route configuration:
user_homepage:
pattern: /user
defaults: { _controller: UserBundle:Default:index }
user_show:
pattern: /user/{id}
defaults: { _controller: UserBundle:Default:show }
requirements:
id: \d+
And this is my very simple controller:
public function showUserAction($id)
{
return $this->render('UserBundle:Default:show.html.twig', array());
}
What is wrong?
The logical name UserBundle:Default:show refers to UserBunde\Controller\DefaultController::showAction you have a method called showUserAction.
Either change the method name to showAction or change the logical name to UserBundle:Default:showUser.
Although not relevant to the example given, this error can also be caused if the controller Action is not public
You're defining your controller function as showUserAction while in the definition your saying it is show[Action].
Either change your route configuration
user_show:
pattern: /user/{id}
defaults: { _controller: UserBundle:Default:showUser }
requirements:
id: \d+
or change your controller signature
public function showAction($id)
{
See if this helps
After big search, this worked for me:
1.- Create CRUDController
// src/Acme/DemoBundle/Controller/CRUDController.php
namespace Acme\DemoBundle\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Inter\PimeBundle\Entity\Empresa;
class CRUDController extends Controller
{
public function publicarAction($id)
{
//code here...
}
}
2.- Create the service
# app/config/config.yml
services:
ebtity.admin.service:
class: Acme\DemoBundle\Admin\EntityAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: group, label: label }
arguments:
- NULL
- Acme\DemoBundle\Entity\Entity
- AcmeDemoBundle:EntityAdmin
3.- Create the template for the action button
{# src/Acme/DemoBundle/Resources/views/CRUD/list__action_publicar.html.twig #}
<a class="btn btn-sm" href="{{ admin.generateObjectUrl('publicar', object) }}">Publicar</a>
4.- Configure route
// src/Acme/DemoBundle/Admin/EntityAdmin.php
namespace Acme\DemoBundle\Admin;
// ...
use Sonata\AdminBundle\Route\RouteCollection;
class EntityAdmin extends Admin
{
// ...
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('engine')
->add('rescueEngine')
->add('createdAt')
->add('_action', 'actions', array(
'actions' => array(
'publicar' => array(
'template' => 'AcmeDemoBundle:CRUD:list__action_publicar.html.twig'
)
)
));
}
protected function configureRoutes(RouteCollection $collection)
{
$collection
->add('publicar',
$this->getRouterIdParameter().'/publicar',
array('_controller' => 'AcmeDemoBundle:CRUD:publicar')
);
}
}
5.- Clear cache
Hope it helps
Not the case here. But there is another related issue:
If you forget the 'Action' suffix all will work. But when you realized that you forget the suffix and then add it ... surprise! Same error as the OP.
The problem here is about caching
Symfony creates two file for caching urls:
AppDevUrlGenerator.php
AppDevUrlMatcher.php
If you change your action name (i.e. adding 'Action' suffix) then that cache info is obsolete.
Solution
php app/console cache:clear
Similar to the accepted answer, if your controller is defined as a service, e.g. (in YAML):
services:
user.default:
class: \UserBundle\DefaultController
And your route uses this controller service:
user_show:
pattern: /user/{id}
defaults: { _controller: user.default:showUserAction }
requirements:
id: \d+
Then it's necessary to name the action method in full including the Action suffix, otherwise you will get the "controller ... is not callable" error.
In my case, i was using symfony 2.
Prior version of symfony maintain method naming convention. Method suffix should contain Action word.
example:
in route yml file the method definition was
docudex_report_make_authorization:
pattern: /make-authorization
defaults: { _controller: DocudexReportBundle:Default:makeAuthorization }
and in the controller the method was
public function makeAuthorization(){}
therefore i was getting the error.
After changing the method name to public function makeAuthorizationAction it worked perfectly.
I would like to share my experience & how I solved it:
I was importing one bundle in an application whose routes were defined using annotations and they were importing fine in application too by using:
auth_bundle_routes:
# loads routes from the given routing file stored in some bundle
resource: '#XyzAuthBundle/Controller/'
type: annotation
But since my bundle controllers were defined using services, Symfony was showing error:
The controller for URI "/facebook/request-code" is not callable.
Controller Xyz\AuthBundle\Controller\FacebookController" has required
constructor arguments and does not exist in the container. Did you
forget to define such a service?
I updated my bundle for routing to use routing.yaml file over annotations and referring to controllers as service::method syntax and then this error is gone.
xyz_auth.facebook_request_code:
path: /facebook/request-code
controller: xyz_auth.controller.facebook_controller::requestCode
Bundle routes must be imported using
auth_bundle_routes:
resource: '#XyzAuthBundle/Resources/config/routing.yaml'
Hope this will save someone's time.
The same issue could happen if your env uses .env.local.php and new changes are added to env files without running composer dump-env
One of the reasons for this error is that you missed to add the word "Action" after the controller's method name. If you think that your routing is OK, check the method name.

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

Resources