So, school has given me this domain which includes quite a lot of program possibilities including symfony, I decided to try and get my website, made in symfony on there, but I can't get the routing to work since it only shows me a message saying that symfony has been installed successfully, I've ran out of options myself, I haven't tried a lot, since I don't know a lot about it, hence the question here.
TL:DR
Symfony only shows the "installed successfully" on remote server (hosted by Neostrada) I'm hoping someone here can help me out.
EDIT: here's a screenshot of the message:
First, I'll post this link here...
read the note at the end of the answer first
Now. I don't know if you know what MVC is, but shortly, it's a structure called Model-View-Controller where the user first contacts the Controller, in symfony also called an Action, which picks an appropriate action function, geatheres Models (via communicating with the database and all that magic) and then sends the whole package to the View (in symfony created in Twig templating framework).
The file structure is made like so:
app //contains main server files
- cache //contains production and dev files, you won't pay much attention to this.
- config //This is where you find the routing.yml, paremeters.yml and other config files
- logs //git logs and other goodies
- Resources //This is where your views are found
bin //Doctrine and goodies, don't pay much attention here for now...
src //This is where the bundles (controllers) are found
- AppBundle //You should get this bundle by default
- Controller //This is where you put your controllers - they have to have a Controller.php sufix (UserController.php)
- Models, Enteties and other folders you want to put in //All custom folders
- YourCustomBundle (note that both are ending with Bundle)
web //This is where you put your css, js etc. files (in js/, css/... subfolders)
Now, if you go to the HomeController in src/AppBundle/Controller. In there you'll see indexAction function. This one triggers the main, index page.
Note the namespace, usings and what the controller class is extending. If you create another action called, for instance myCustomAction(Request $request), you'll create an action ready to take in orders. But from where? Go to app/config/routing.yml, you'll probably see something like:
app:
resource: "#AppBundle/Controller/"
type: annotation
Above it add:
page:
path: "/"
defaults: { _controller: AppBundle:Home:myCustom }
//note that I first called the bundle (AppBundle, then the HomeController (without the
//Controller part), then the action (no Action part neither)
Now you got yourself a path to your action! Now simply return a view in the controller (look at the indexAction), should be something like:
$this->render('home/mycustom.html.twig');
in the myCustomAction function. After that create a mycustom.html.twig file in the app/Resources/views/home/ folder and that's your view. Note that Twig has a bit different syntax than PHP.
note: I highly suggest you learn from the official symfony website. This is all TL/DR (too long, didn't read) style of writing. It's explained a lot better there with details into why you're doing something. What I wrote might or might not work for you, because of the speed in which I explained the concept! Also if you're not familiar with MVC, I suggest you learn the logic behind it first, before rushing into Symfony.
Related
I'm kind of new to Prestashop and I decided to practice on a project that's completed already (Prestashop 1.6.1.18). The thing is the project is pretty large and sometimes I get lost in the folder structure. I come from Codeigniter and Symfony and thought that might have been useful thinking Prestashop too had a similar URL->Controller->View mechanics but so far I've been going on for a while now trying to find the controller (and controller function) which handles this specific URL:
http://localhost/content/discount-products
I would appreciate some insight on how URL end points make their way to a function in a controller. Are they registered somewhere (Routes.php in Codeigniter, config files in Symfony). Should I be able to now this from that URL alone?
Thanx
Use this code on hookDisplayHeader
p($this->context->controller);
This code gives you all information about current controller on both BO and FO
Coming from a straight PHP and Drupal background, I am recently learning the Symfony2 framework. Currently I am in the routing chapter of the book. This is probably a simple question.
What are some real world use cases for why one would want to generate URLs in a Symfony app? I understand the code but I'm having a bit of trouble determining its practical applications.
I'm referring to this section should you need a refresher.
As always, thank you!
P.S. Symfony is amazing. :)
Basically, you need to generate a URL whenever you need to link to anywhere in your application.
Let's say that you have an application that needs to manage some users. This means that you will probably have URLs like /user/create, /user/edit/(user id) and /user/remove/(user id).
Whenever you display a link to edit a user you need to know on what URL you can find the page that allows you to edit a user. So you need to link to /user/edit/(user id). One solution would be to have this as a fixed link so that in your code you could just write
edit this user
But what if you want to change this URL scheme? Let's say someone is unhappy with the term "user", after all the humans managed by this system are not only users, they are actually "person"s! So now you need to change all the URLs containing "user". Probably there are quite a few places in your app where you have had to hardcode these URLs and now you will need to find and change all of them. Eugh.
But fear not, for Symfony routing comes to the rescue!
Instead of hardcoding these URLs, we can simply let the Symfony router generate them for us. This means that we first need to tell Symfony which routes we have, e.g. by adding the following YAML code to our routes config file:
user_edit:
path: /user/edit/{userId}
defaults: { _controller: AppBundle:User:edit }
requirements:
userId: \d+
This tells our application "Okay, whenever somebody requests a page that looks like /user/edit/{userId}, then you need to call the editAction method in our UserController class in the AppBundle namespace and you need to pass the userId as a parameter. Oh, and also you should only call the controller if userId is a valid integer with at least one number."
So this is how Symfony knows how to map URLs to controllers. But the goodness that comes along with it is that we can use this information for the reverse way as well.
Usually, in our application we do not really care about what the URL looks like for a certain action we want to perform. All we know is that when clicking a certain link, then the browser should jump to a page that allows me to edit a user. And since we just defined a route that takes us right there, we can have Symfony generate the correct URL to achieve just that.
So in your view you can now discard the hardcoded URL from earlier and instead replace it with a route generated by the Symfony router:
edit this user
Now whenever you need to change what the URL actually looks like all you need to do is edit your routing config and not a lot of separate views.
Because, imagine you want to change a given page URL and you've hardcoded it in 10 Twig templates. You will have to modify all these files. On the opposite, when using the routing component:
You would only have to change the URL where the route is defined.
The routing component "takes" care of the current environment you are using (dev, prod...)
Also note that is a bad practice to "switch environment", a typical issue is to hardcode an URL in a Javascript. In this case you can expose your Symfony2 routes in the Javascript by using a bundle like FOSJsRoutingBundle.
I almost immediately realized their use and now I feel silly. :) For those who stop by this question in the future:
Notes about Generating URLs:
Similar to the Drupal l() function, sometimes you need to generate links inside your application based on a variety of parameters.
You don't always want to hardcode your links in case you decide to change paths sometime down the line.
In summary: Think of this as an alternative to using straight anchor tags with href elements all over the app and, instead, keeping things dynamic.
Use case
In the project I'm working I use generateUrl to redirect the user
after creating, editing an entity.
For example after creating a Service entity, I redirect the user to the view
of the just created Service.
Controller
return $this->redirect($this->generateUrl('myentity_view', array('id'=> $id)));
Additional note
In twig files, you can use the path function which call the routing component and generate url with given route name and parameters.
Well im just starting a new project and i choosed symfony2 as the MVC framework for it. i want first to start this project by creating a BASE with a modular architecture, i mean an empty application that contains the main and common services like (navigation tree management , activation/desactivation of modules using database , logger..) or any other global functionalites that may come later after detailed conception. my modules will later be in bundles.
what im thinking of is a Core controller that would receive all requests, do all the treatement needed, init/change the services that the modules will use depending on configurations (files or DB) and cache (session/globals) then call the called controller and return the response. to do that i have to change the kernel to always dispatch toward this controller and give the action and the controller that the user called to it.
i did some project in symfony using only the standard edition and this is the first time trying to do internal customization so i dont have a lot of experience. if anyone think that my idea is bad and have other suggestions plz give them ill be extremly greatful !
Edit : i may specify that this BASE is just tests to find the perfect modular architecture so any other idea related to modularity in symfony would be a big help ! thanks
The kernel itself has nothing to do with the controller. That's all handled by the ControllerResolver. If you always want to handle each incomming request using the same controller, you can create your own ControllerResolver to Always return an instance of that controller. See http://symfony.com/doc/current/components/http_kernel/introduction.html#resolve-the-controller for more information about controller resolving.
However, I would recommend not to do it this way. Controllers should be very tin layers between User land and Logic land. All heavy things should be done in the logic layer, in services. And if you use services, you can better register them as listeners to the kernel.controller event than to call them from within a controller. See also http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html
I've recently started to learn Symfony2, based on a job opportunity. I've got some of the basics down, but I'm strugging with the #template() section of annotation routing.
I've been able to use annotations to route the right url to the right controller method, but the template I put in #template() never seem to work.
Basically, what I'm after is an explanation of what goes into it. I've seen in the official documentation:
#Template("SensioBlogBundle:Post:show.html.twig")
But I don't understand what that param is. I get the ending part is literally a filename, but what are the first two components separated by colons? I've tried a bunch of different things to try and understand, but I haven't been able to make it connect to a new template file I've placed in views/main/index.html.twig of my bundle's resources. I've tried
#Template("BlogBundle:main:index.html.twig")
With no success. (BlogBundle is the folder all my code resides in, within my bundle)
The rest of the documentation seems to make sense, it's just this one param never seems to get a good explanation.
Thank you.
SensioBlogBundle:Post:show.html.twig mean this path: src/Sensio/BlogBundle/Resources/views/Post/show.html.twig
The bundle:controller:view is Symfony2 standard notation and, in case of template or render of views, have to be interpretated as I explain.
I'm playing around with Laravel and am struggling to convert an application (simple blog) I made within the standard application layout into a bundle.
Having spent the last half hour reading up on namespacing and working through related error messages, I've got my model loaded and I've got it talking to my separate authorisation bundle. So the next problem is views.
Latest error message is:
View [home] doesn't exist.
Location:
/var/www/lara.dev/laravel/view.php on line 170
I have basically recreated the application structure in my bundle, with a views directory containing the view I am trying to load (bundles/blog/views/home.blade.php) from my bundles controller (bundles/blog/controllers/home.php)
I previously used the following line to load the view :
return View::make('home')->with('posts', $posts);
What do I have to do to make my views load as normal from within the bundles? Also, if it's obvious from my question that I'm missing something here then please enlighten me. I'm pretty new to OO in PHP
You simply namespace the view with your bundle name.
return View::make('bundlename::home')->with('posts', $posts);
This same approach works for almost anything. You should give the Bundle Docs a full read, specially the Using Bundles section. Bundles & Class Autoloading may also be of your interest.