I am currently learning Symfony2 and so far I really like the framework. However, I have a situation that I don't know how to solve.
I have a Symfony2 application that has two bundles: Frontend (prefix: /) and Admin (prefix: /admin; this prefix is changeable). Now, I am trying to add AngularJS application in HTML5 mode (the URLs don't have a hash).
Here is how I want to achieve this:
Create a "sub-bundle" that will have the following prefix: /admin/_api. Here are two examples on how they will look like: POST /admin/_api/users or GET /admin/_api/users/10
Create a 404 page in Admin bundle that will load the AngularJS template.
The reason for doing this is simple. When I open the path /admin/users/10 for the first time, the AdminBundle error page will open the AngularJS template. And AngularJS router will route /admin/users/10 to the necessary controller. Then, the AngularJS controller will make a request to GET /admin/_api/users/10 and send the returned JSON data from the server to the AngularJS view.
For the first problem, even if the solution is not the most elegant one, I can add prefixes in the AppBundle's configuration. But my main problem is the second one. How can I create a bundle based error page?
Related
So I am working on a project and I have decided to use php laravel framework, but when it comes to things like creating a new page and dealing with page redirects etc, am I right in thinking that all these will be handled in the route/web.php file so all the pages for my application will be defined in the route along with the view ?
I was thinking what if my application grows to have dozens of pages is it best practice to define each one on the route or are there better ways to handle this?
Yes, the best way to do routing is laravel to have each route for . each page, the only exception is when you have dynamic routes, for example, if you have a route that checks for users id or category for some product etc and it looks something like this Route::get("/product/{$category_id}","FrontController#methodForGetingProduct").
And later in Controller, you define what will you send and receive of the information and what view should be returned.
you can follow this type routing .......
I have a website written using Kohana Framework. The way I've been doing things in my controller is like so:
public function action_about() {
$this->template->body = View::factory('main');
$this->template->body->set('main', 'about');
}
I've gone through a React tutorial where react router is used. To go from page to page, I had a function like so:
goToAbout(event) {
event.preventDefault();
this.context.router.transitionTo('about');
}
The way I understand it, if I'm adding React to an MVC framework, it should simply replace the V. That would mean I should still be using my framework's routing and controller. This is where I'm stuck. What am I missing?
In this circumstance, it augments the "View" layer of your application. You still have to render a view with your server side framework, but you can let React take care of everything after that. For example, suppose your react app has three routes "/users", "/users/:id", "/users/new". The routing in your server side framework (Kohana) must still respond to these routes.
However, instead of rendering a custom view for each route, you would render out the same view in each controller - a view that includes the Javascript to mount your React app.
Once your React app is mounted to the page, it will take care of parsing the URL and mounting the correct component.
PS. Alternatively, if your server side framework allows it, you might also map each URL to the same controller to render the aforementioned view.
I have set up a simple app for now using ReactJS for the frontend and Laravel(5.1) for the backend.
All is OK but I woudl like to make the URLS look normal, as in the removal of the '/#' from the URL.
So
example.co.uk/#/about
becomes
example.co.uk/about
This is not an issue as I achieve this, but when I do it activate it, then the Laravel routing kicks in and flags a route error.
Is it possible to prevent Laravel form activating / doing this so that ReactJS takes over and works.
If so it would also be nice, so that if a Laravel route is set / wanted then it does use that one.
EG: These are ReactJS routes that would have used the '/#'
example.co.uk/about
example.co.uk/details
example.co.uk/listings
Then if I got to the following URL's then they are controlled by Laravel's routing?
example.co.uk/api/...
example.co.uk/admin/
Thanks
You need a catch-all route that will execute the same main controller action regardless the URL. Add the following to your routes.php
Route::get('{path?}', 'Controller#action')->where('path', '.*');
This way all URLs will go to Controller#action, that should display the base view for your application - the one that runs the ReactJS application.
I need some help. I'm new to laravel and I'm experiencing some weird things.
I have a "users" resource inside views/users. Inside it are index.blade.php andcreate.blade.php.
My app url is http://laravel.dev/ so basically if I need to access the "Create Users" form I will go to http://laravel.dev/users/create the view loads fine but when I tried to click a link on my navbar it automatically adds "users" on the url even if there's no "users" in the nav url link. Example.
Dashboard
When I click it, it will add "users" in the beginning of the url automatically making it users/dashboard and as expected I get a 404.
My navbar is being loaded from other php file in my header.php
Here's my route
/*Route for Users*/
Route::resource('users', 'UsersController');
I would suspect that you are using page relative links, correct? In a nav I've always found it best to use root specific links so "/dashboard" vs. "dashboard."
If you are new to Larvel you may want to take a serious look at the rooting options before going to far into your project just because if you decided to go a different way, namely routing through the controllers (http://laravel.com/docs/controllers#resource-controllers) With a large app it can be a far better way to go, but the refactoring can be tough after to you have a complex enough app to justify it.
I have two subdomains pointing to the "web" directory in my Symfony 1.4 implementation and would like to route to certain modules/actions based which subdomain was used to arrive at the site
sub1.domain.com --> module1/action
sub2.domain.com --> module2/action
Is there an easy way to do this in routing.yml? Customize index.php, parsing the host for subdomain?
Take a look at this chapter in More With Symfony. It does essentially the same thing by creating a custom route that checks the subdomain... It uses the DB but you could omit that if you dont need to hit the DB.
I'd rather write an execution filter or custom routing class.
Check out this example: http://www.symfony-project.org/more-with-symfony/1_4/en/02-Advanced-Routing