Laravel 4: Dynamic db selection using Capsule - laravel

Just trying to understand how Capsule works in laravel. Everything works correctly where it is defined, however, as soon as a new page is opened it again take backs the old connection information instead of the new connection which is defined.
I believed setAsGlobal() basically makes it available for that particular session atleast, however, I guess I am conceptually wrong here.
It would be really nice if someone can guide through the right way to make the new database connection available globally VIA CAPSULE, there are various other ways, however this seems more promising.
It would be really nice if someone could explain the following commands in more simpler way (the comments are written as per in the documentation, however, something above that would be really nice):
// Set the event dispatcher used by Eloquent models... (optional)
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
Any help would really be appreciated. Thank you.

Check my answer at https://stackoverflow.com/a/34837068/1216285 to use Capsule out of Laravel scope in your app. Since all the laravel components are decoupled and served as independent components in laravel project, you can use them in your app easily.

Related

Vaadin UI between sessions

When I am creating a new session (or try to access from an other computer) in Vaadin Flow I get this error:
Can't move a node from one state tree to another
From this link, I read something about UI and getUIId().
However, I don't understand how I should change my application in order to fix the error.
As Denis mentioned in the forum post you linked, wrong scope sounds like the most likely culprit. In other words, you are trying to use the exact same component instance in two different UIs, when both UIs should have their own instance. It's not possible to use the same instance in two places at the same time.
You can find the documentation for Vaadin Spring scopes here: https://vaadin.com/docs/latest/flow/integrations/spring/scopes
One possible cause of errors such as that is that if you're storing a Component in a static variable. You shouldn't do that - a Component instance can only belong to a single UI. A single UI in turn (in practice) means a single browser tab.

Where to put ajax request in Ember tutorial app?

I want to add "Weather: 24C" to the rental-listing component of the super-rentals tutorial app.
Where would be the "best-practices" place to put this ajax request?
Ember.$.getJSON(`http://api.openweathermap.org/data/2.5/weather?q=${location}&APPID=${apiKey}`)
.then(function(json) {
return JSON.parse(json).main.temp;
});
Do I need to add a component, add a model, add a service, add a second adapter, modify the existing adapter? Something else? All of these? Is the problem that the tutorial uses Mirage? I ask this because when I think I'm getting close, I get an error like this:
Mirage: Your Ember app tried to GET
'http://api.openweathermap.org/data/2.5/weather?q=london&APPID=5432',
but there was no route defined to handle this request.
Define a route that matches this path in your
mirage/config.js file. Did you forget to add your namespace?
You need to configure mirage to allow you making calls to outside in case mirage is active; what I mean is using this.passthrough function within mirage/config.js, that is explained in api documentation quite well.
Regarding your question about where to make the remote call is; it depends:
If you need the data from the server to arrive in case a route is about to open; you should prefer putting it within model hook of the corresponding route.
If you intend to develop a component that is to be reused from within different routes or even from within different applications with the same remote call over and over again; you can consider putting the ajax remote call to a component. Even if that is not a very common case usually; it might be the case that a component itself should be wrapped up to fetch the data and display it by itself for reusing in different places; there is nothing that prevents you to do so. However by usually applying data-down action-up principle; generally the remote calls fall into routes or controllers.
Whether using an ember-data model is another thing to consider. If you intend to use ember-data; you should not directly use Ember.$.ajax but rather be using store provided by ember-data and perhaps providing your custom adapter/serializer to convert data to the format ember-data accepts in case the server do not match to the formats that ember-data accepts. In summary; you do not need to use models if you use pure ajax as you do in this question.

Hidden Laravel Methods (5.1)

First, I have IDE helper, and the php storm plugin. I tried the Gist pre made too. There are some similar questions, but no one seems to get answers. I'll probably poke laracasts and ide helper bug list if I don't get anything here.
So I'm following along to some of the into laracasts, and the guy keeps using methods that are not defined as far as I can tell. Situation:
I created a eloquent model called Article. It extends
Illuminate\Database\Eloquent\Model
So now I have App\Article and I can call any of the methods available to model. For example:
$article = \App\Article::all();
PHPStorm is happy. He keeps pulling stuff like ::find() or ::findOrFail()
It's in the docs
I just don't under stand how that works, I don't see the methods defined in model. If this is what ide helper is supposed to fix, then I'm not certain it's working correctly. I can RTFM, I'm pretty sure I followed the directions to a tee.
Ya know, I just found it. I see this question out and about, so I'll answer it here.
https://github.com/barryvdh/laravel-ide-helper/issues/248#issuecomment-131503475
Fixed find or fail for me. find is still MIA. I'm surprised laravel doesn't support their code base in the forms of plugins or dedicated IDE's bit more. It's all just people out there creating a community and moving the world forward so I can't complain too much.
It works because Model implements __callStatic() which dispatches it to itself on a new instance: __callStatic() implementation on Model
It creates a new instance (new static) of the model in question and dispatches the statically called method on the instance.
Effectively, Model::foo($bar) is the same as (new Model)->foo($bar).

MVC 3/Design Patterns issue

I made a settings page for my website. On this page the user is presented with a bunch of site wide settings they can manipulate. I made it so when the user selects a setting the page will automatically run an ajax request to send the setting to the database. My question is in how I do this.
At first I just did calls to the repository. One call to get the data back, put it into a ViewModel then give that ViewModel to the View and the ajax controller just sent the settings back to the database. This way seemed like the best at first especially for unit testing purposes since I could just pass in a fake repository if needed. Then for the user to get a setting they just called the repository and pass in the setting name they want.
Then I had a bright idea. I made a singleton class called SiteWideSettings and each possible setting on the site was a property of the site. When SiteSettings is called for the first time all of the settings are loaded. When Set is called on any of the properties it will call the repository function to send the setting. Now with my Settings view I'm just passing in SiteWideViewOptions.Current and on the ajax call I'm updating the property that was changed. This is working for me however it's not very unit testable since I can't really pass in a repository to a singleton's constructor since its constructor is private. What I currently have is working fine but I just don't feel like it's the best solution and unit testing isn't really possible here.
I'm thinking of one of the following but not sure which is the best.
Add a Repository property to the SiteWideSettings class
Add a function to the SiteWideSettings class to pass in a repository
Not use a singleton for this at all and just go back to what I was doing before I had this idea
Any comment on this would be greatly appreciated.
Note: I know. I know I'm doing unit testing wrong in this case because I didn't write my test first so please don't scold me for that.. I have already scolded myself and with my next task I won't do it again I promise :)
"Then I had a bright idea. I made a singleton class called
SiteWideSettings and..."
This sounds like a bad idea. Let your database be ground-truth for what the settings are, not some in-memory cache that you now need to keep up to date. Let your ORM do caching if you need it for performance otherwise you are just adding problems especially if you now try to run your site on more than one server.
If you want to simplify the controller so it has less 'set-up' and 'tear-down' code in it, use an IOC (e.g. Autofac) and inject any dependencies you need (e.g. a DataContext or a Repository) on a per-http-request basis.
Your action methods are now easier to test since you can simply instantiate your controller (injecting the dependencies manually using its constructor) and then call your method.

How to trace CakePHP's inner workflow

Short description
I'm getting used to CakePHP right now and am wondering about how to get more debug-information about what is happening inside the framework.
Let me please explain my situation a little more detailed
As you know CakePHP does a lot for you without putting you into the need to write additional code. One example is the handling of models.
I just created a model User and added validation-rules (no other methods). As described in the API methods like save will just work.
After that I created the needed controller and view to add a new user. When I try to add a user from the view I just receive the flash-message The user could not be created. Please, try again. No validation-violations are flashed.
I also did set the debug-level to 2: Configure::write('debug', 2); but do not receive any errors. The error.log inside \tmp\logs is also empty.
I really do want to learn how to solve those issues in the future.
So what else can I do to debug / display inner processes of cake?
Thank you very much for your help!
DebugKit is an official plugin that gives you lots of information on the request, queries and variables produced by Cake:
https://github.com/cakephp/debug_kit
You can also use trace() and other methods in the Debugger to show what is being executed in the background:
http://book.cakephp.org/2.0/en/development/debugging.html
Use a PHP IDE with an integrated debugger. That will allow you to follow execution line by line as it is executed and even inspect variable values as you go. Netbeans is a free one.

Resources