Laravel - Converting application to bundle - How to load a view - php

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.

Related

Symfony only showing "symfony has been installed" on remote server

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.

From project without framework to zend framework 2

I started a project without using any framework.
My project structure was something like this:
A folder with classes to access my DB
A folder with classes for business logic
A folder with classes that can help me do other stuff
A folder with php pages that can be called inside pages(eg: navbar, footer, etc)
On root I had my php pages
So, calling all classes was preety easy. Now i am creating a new zf2 project and want to move all my work to this new project.I already setup the layouts and content of each page but i'm having some problems adding my custom classes. Should I use other kind of organization? Where can/should create my custom classes?
I also have some "php files" where I check some user info and depending on that, show him oriented advertisement. On my old project I was just calling it with "require"... can I do this on ZF2?/ is this the proper way to do it?**
Any help would be appreciated.
For anyone having the same question.
I created my custom library inside the vendor folder.
Here are some useful links:
http://code.tutsplus.com/tutorials/psr-duh--net-31061
http://ulaptech.blogspot.pt/2014/02/adding-3rd-party-libraries-to-zend.html
ZF2 follows the MVC (Model View Controller) design pattern.
So your directory structure looks like
module
Entity.php
EntityInterface.php
Table.php
TableInterface.php
TableFactory.php
The basic access methods are in Table.php
The mapping between columns and array is in Entity.php
If you would consult the tutorial Album application on the Zendframework website, you will see a working example.

Should I edit the appkernel to use a Core controller

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

Override Laravel 4 Package view

I want to create my custom CMS and I'd like to create a user package in which I will have a controller with showProfile() function. But the problem is I'd like to easily edit this profile view. So I want to know if there is a way to create cascade view. Like if there is no file in app/views/ then vendor/vendor/package/src/views will be loaded. I hope you got this idea :)
EDIT:
I managed to make it work. I had to register new namespace for views in my ServiceProvider.
I put this code to ServiceProvider:
\View::addNamespace('cmscore',array(app_path()./'views/packages/zaalbarxx/cmscore');
Where zaalbarxx/cmscore is vendor/package and cmscore is a namespace I can use later in controller like View::make('cmscore::index'). I added this code in boot() method BEFORE $this->package() so this way app/views are prioritized over package/views. Works brilliant.
It is already possible, however the structure would be it look into vendor/package-name/src/views by default, but if there is the equivalent in app/views/packages/package-name/ that would be chosen.
As stated, you should be able to load package views already.
However, you can add more view locations in the array found in app/config/view.php.
Additionally view paths can be added at run-time with the addLocation() method found in the FileViewFinder class.
Using that method that in a service provider would look like:
$app['view.finder']->addLocation('/path/to/views');
Or anywhere in your app:
App::make('view.finder')->addLocation('/path/to/views');
Also note, I answered this question on cacheing view output recently, which might help you see how extending some portions of the View package might work if you choose to go down that route.
You don't need to program this behavior in, if you read the laravel code you will see that this is built in...
Packages by default will first look in and
app/views/packages/package-name/ (all in lowercase! even if package or author have caps! goes unnoticed on windows and then on linux you will bump your head against the wall! )
and if the customer app view doesn't exist the package views will load from the package itself inside:
vendor/author/package-name/src/views

Zend Framework 2 - Set Template Path for Module

I googled alot and couldn't come up with an answer...
I'm using the tutorial-skeleton application. It automatically includes under 'view/album/album' the html files corresponding to my actions like add or index.
I'm using a submodule and the standard loading won't find my html-files. I followed this guide for setting a custom template path. This works for the index because here I use a ViewModel instance.
But my add/delete/edit actions just return an array like this one.
Is there a way to tell Zend that it should use a different directory to look for the views?
PS: I also tried this injectTemplate approach but no luck. It just sets the Controller namespace/path which is ok in my case.
This was an project specific issue...
I used MasterData as top namespace. When creating the directory tree in my module\MasterData\view I wrote masterdata instead of master-data. This caused the not finding of my views.
A dumb one... I know.

Categories