Toza
Toza

Reputation: 1358

Combining AngularJS and Symfony2 - routing and non-ajax page access

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)

Upvotes: 0

Views: 129

Answers (2)

Toza
Toza

Reputation: 1358

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.

Upvotes: 1

chalasr
chalasr

Reputation: 13167

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.

Upvotes: 2

Related Questions