Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use routes #11

Open
vicenteherrera opened this issue Jun 12, 2017 · 0 comments
Open

How to use routes #11

vicenteherrera opened this issue Jun 12, 2017 · 0 comments

Comments

@vicenteherrera
Copy link
Contributor

Hi Asier,

Trying to learn to use Sense, I have some doubts that I don't know if are caused by me not know how to do something, or lacking some info on the documentation (recently sent a pull request extending it), or maybe a bug.

So I'm creating a test controller, but instead of using:

class TestController extends AbstractController {
/**
 * @Route("/demo/{name}", method="GET", name="test_route")
 */
public function demoAction(Request $request, WP_Query $query) {

As I’m working on a namespace like in the examples, I have to use fully qualified classes for parameter types

<?php
namespace Simettric\Plugin\Controller;
 
use Simettric\Sense\Annotations\Route; //Needed for the @Route comment anotation
use Simettric\Sense\Controller\AbstractController;
 
class TestController extends AbstractController {
/**
 * @Route("/demo/{name}", method="GET", name="test_route")
 */
 public function demoAction(\Symfony\Component\HttpFoundation\Request $request, \WP_Query $query) {

Then, trying to get the $name as a parameter of the method, it doesn't work... it always gives me an error saying that only 2 parameters are expected, and first is expected to be of Request class:

class TestController extends AbstractController {
/**
 * @Route("/demo/{name}", method="GET", name="test_route")
 */
public function demoAction($name, \Symfony\Component\HttpFoundation\Request $request, \WP_Query $query) {
	//doesn't work as intended

So instead of that, this is what works for me:

/**
 * @Route("/demo/{name}", method="GET", name="test_route")
 */
public function demoAction(\Symfony\Component\HttpFoundation\Request $request, \WP_Query $query) {
	$name = $request->get('name');

Then, trying to use the requirements clause in the Route, I've already figured out that instead of how it's told in documentation, you have to use like in the following example... but then, request->get('name') doesn't work any more.

class TestController extends AbstractController {
 
/**
 * @Route("/demo/{name}", requirements={"name":"\w+"}, method="GET", name="test_route")
 */
public function demoAction(\Symfony\Component\HttpFoundation\Request $request, \WP_Query $query) {

	$name = $request->get('name');
	//Now $name is empty

If you tell me a working example of use of requirements and passing an URL parameter, I’ll update the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant