Override Laravel 4 Package view - php

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

Related

Add custom Eloquent ORM models to WHMCS version 6

WHMCS version 6 uses the Eloquent model.
Their using models documentation clearly states how to access the WHMCS database.
The problem I am experiencing is that I want to access my own model. I have multiple models, some that access WHMCS directly, and other that access a completely separate database.
To use their model, you do this:
...
use WHMCS\User\Client;
...
I tried simply to do this:
...
use WHMCS\User\Client;
use Radius\User\Logon;
...
I added my models to $root_folder/includes/classes/Radius/User/Logon.php mimicking the folder structure of WHMCS.
However, I get an error Fatal error: Class 'Radius\User\Logon' not found in /var/www/vhosts/snowball.co.za/wh6.snowball.co.za/modules/servers/radius/radius.php on line 543
I fully suspect I have to update Composer to recognise my own models, but I am not sure. What I do needs to be fully integrated with WHMCS and it must not break anything.
Does anyone have advice?
You may need to update your autoloaders.
Thank you for posting the solution - others will no doubt appreciate it.

How can I overwrite a core laravel method?

Here is what I'm trying to do:
We are using Laravel 4.2 in our project, and are using the frameworks' Password::remind functionality to send emails for password reset.
The problem is that the team wants all the email templates to be located inside the database instead of the views folder, so I will have to somehow pass a string to the Illuminate\Auth\Reminders\PasswordBroker::sendReminder method.
How can I override this class in Laravel so I can make this thing work? I'm currently a Laravel newbie so I don't yet fully understand how the framework works...
It isn't easy to override the classes simply. Whole laravel mail is based on views. But you can probably create a workaround to achieve you're goal.
For this you've to make the required views. In a service provider or in you're route file you make a view composer. With that view composer you retrieve your data from the database and the only thing you do in the view is printing the value.
View::composer(array('reminders.password','reminders.other'), function($view)
{
$view->with('html', RemindersRepository::getHtml());
});
Or something like that. Now in you're view print {{$html}} and it works!
Edit:
For you're information a view composer is something like a event listener. When the view is loaded, the callback function of the composer is loaded. In that callback function you can pass a extra variable with some contents. In the view you can print this value added in the composer.
Here is a basic guide on how to override/extend core functionality in Laravel:
You can create a folder in app/start/ and then create your own class to override the default behavior like NewReminderServiceProvider.php Then you extend the core functionality in question:
class NewReminderServiceProvider extends Illuminate\Auth\Reminders\ReminderServiceProvider {}
then overwrite or extend the registerPasswordBroker.
In the parent you are extending, you will see where it sets the view:
$view = $app['config']['auth.reminder.email'];
change that to be database driven however you want.
then last of all you have to swap out the ReminderServiceProvider with your NewReminderServiceProvider in your app/config/app.php and you are good to go. This will work with almost any core functionality. Replace or extend blade, auth, etc.

CodeIgniter: where should I put custom utils?

I'm using CodeIgniter and I have a few utils that I want each controller (that is, each controller file) to have access to.
The question is: where to put these?
I thought of helpers, but the CI documentation talks only about extending existing helpers, not making your own. I'm sure that doesn't mean I can't make my own helpers, but it does mean I don't know how they should be built (procedural? Methods of the global CI instance? etc)
I also considered hooks, but this is a poor fit I think as I'm not extending core functionality.
Or is there some other way I'm missing?
It's been a while since I've done this but I believe I used two approaches.
Creating a new, custom helper that goes into /application/helpers, following steps noted from this answer: CodeIgniter: Create new helper?
Creating a new library class into /application/libraries which I also activate in the autoload configuration found in /applications/config/autoload.php. This way it's always available to my controllers when I need it. CI has good documentation on this one (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html).
I did it simply by adding a file to the application/helpers folder (maybe I created that folder - I can't remember) and then loading them in the usual way.

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

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.

Symfony - What is required to create a plugin?

I've been going through the Symfony documentation in order to find out how to create a plugin. However, the two tutorials seem to give a lot of extra information (for example models etc).
What I'd like to know is, what is the absolute minimum requirement in order to get a controller and template working from a plugin directory?
For example, just an index action and a corresponding 'Hello World' template.
Also, is the routing for this automatic or do I have to manually change something?
Any advice appreciated.
Thanks.
To do what youre askign you would need the following:
MyPlugin/
modules/
my_module/
actions/
actions.class.php
templates/
indexSuccess.php
You would then need to enable the plugin in you ProjectConfiguration and also enable the module in your settings.yml for any apps you want to use it.
Routing is not automatic. You need to add routes manually to routing.yml or you can create a listener and appends/prepends the routes when routing.load_configuration is fired. USing the second option would also imply creating a PluginConfiguration class where you listeners connect to the event via the event dispatcher.
Basically a Plugin follows the same basic structure as an application - except pretty much everything is optional. Whether or not you need to do somethign really depends on what your plugin does. Also you might want to take a look at using sfTaskExtraPlugin it has a task for generating a basic plugin skeleton and a plugin module skeleton.
some examples
enable the plugin in you ProjectConfiguration
go to 'core\config\ProjectConfiguration.class.php' and add next code in setup()
$this->enablePlugins('MyPlugin');
enable the module in your settings.yml
all:
.settings:
enabled_modules: [my_module]

Categories