0

This question is actually consisted of 2 questions.

One being how can I create a certain amount of routes, but all other routes call one HTML page (basically, I want Symfony2 to take in certain URL's, but everything else is being left to AngularJS).

get_user:
    path:     /api/getuser
    defaults: { _controller: AcmeDemoBundle:Api:GetUser }
create_user:
    path:     /api/createuser
    defaults: { _controller: AcmeDemoBundle:Api:CreateUser }
...
other:
    path      [[ what to put in? ]]
    defaults: { _controller: AcmeDemoBundle:Main:GetPage }

Any url afterwards, angular should take in with ngRoute, through the backends other route.

Now the second question I have is, is it possible to restrict all routes (get_user, create_user...), except for the other route for any kind of call that isn't AJAX? For those who know ASP.NET, I want the result of naming a file _file.cshtml (you can't access it directly, but only through AJAX calls)

2 Answers 2

2

I'm not very sure I understand what you need.

You can simply create angularJS routes and write Symfony2 routes on same or other URL, but not with the same method.

If you really want define a route for multiple URL, you can us parameters as URI parts, like this :

other
    pattern:   /{first}/{page}
    defaults:  {  _controller: AcmeDemoBundle:Api:GetPage }
    requirements:
        first: keyword|secondKeyword
        two:  \d+

In this example, the first argument of the url can be one of the keywords, and second must be validated by the regular expression.

Then, if you want restrict your route to AJAX calls, use a condition :

create_user:
    path:     /api/createuser
    defaults: { _controller: AcmeDemoBundle:Api:CreateUser }
    condition: "request.isXmlHttpRequest()"

Or restrict your route to specific method(s) :

create_user:
    path:     /api/createuser
    defaults: { _controller: AcmeDemoBundle:Api:CreateUser }
    methods: POST

But I think you have to define it for each route.

Sign up to request clarification or add additional context in comments.

2 Comments

The second answer is good, thanks! But I doubt you understood what I want for the first question. I want all URL's, other than the ones defined above the 'other' route, even made up ones ('localhost:8000/mymade/up/url' or even just 'localhost:8000') to all lead to one controller action.
I found a solution for the first question and posted it below, if you're interested.
1

I'm accepting @chalas_r 's answer, as it does solve some of my concerns, But for those who run into this question, this is the solution to the first question I had: RIGHT HERE

To make this short, this is the other route I have now:

other:
    pattern: /{first}
    defaults: { _controller: AppBundle:Default:index }
    requirements:
        first: ".+"

requirements: first: ".+" basically overrites the default [^/]+ value which sepearates URL's by a / char. So now I can make up my own routes, like:

localhost:8000/my/route/i/just/made/up

and it'll still take me to the AppBundle:Default:index controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.