How do you find the underlying class behind a Laravel facade? - php

For example - this function uses a facade:
File::get('path/to/file.txt');
It turns out the underlying class that actually supplies File::get is Illuminate\Filesystem\Filesystem
I looked at the Laravel 4.2 documentation - thats the version Im using - and also the api reference but I couldn't find anything that would explain how to someone who didn't know in advance how to find the "real" class to a facade.
this tutorial on Laravel facades gives a method of doing it which involves
finding the File class
looking to see that it extends class Facade
following the code through the Facade#__callstatic() method
tracing the behaviour of __callstatic(), resolveFacadeInstance() when getFacadeAccessor() returns string files
etc, etc
... too long / many steps to post
This is a good demonstration of whats going on, but I wouldn't want to do this regularly.
My question is, knowing that the "facaded classes" you use in your app dont necessarily have a the same name or some convention to help you search the filesystem, how can someone - who doesn't know in advance what the underlying class is - find the underlying class for a laravel facade?

This is a nice resource: https://laravel.com/docs/facades#facade-class-reference other than that make sure to install some kind of intellisense plugin for whatever editor you happen to be using. Most of them allow you to Ctrl+Right-Click on a class/method and go to the definition.

It seems that you can use getFacadeRoot(). For example, to find out what's behind the Mail facade:
get_class(Mail::getFacadeRoot());
// in my case returns 'Illuminate\Mail\Mailer'

Related

How to create router for a component in joomla?

How to create router file for a component of joomla. I am using Sef
url in particular
I was having the same issue and after trawling through the internet and only ever coming across answers like the one seen here ("see the documentation") which is really unhelpful in my opinion and of all the documentation that page is the most unhelpful.
Anyway I firstly ended up giving up on the "component router", I could get it to easily work to build routes but found that it wouldn't parse anything and without having both working, it was pointless continuing.
In the end I decided going down the plugin path was the answer and found this really good plugin here from Daniel Calviño Sánchez.
I then ended up finally coming across some joomla documentation here that IS perfect and will get you exactly where you need to go.
I personally think that the joomla router needs alot of work and saw there are lots of ideas from people wanting it upgraded. I found using a plugin in the end was the easiest path and was the better solution all round.
I would be happy to discuss with anyone as to why my component router wouldn't call its parse method as if that actually worked, it would've been my first choice.
Hope this helps.
Unfortunately documentation didn't provide a way to register my router so I found an alternative solution.
The router.php is actually a Joomla\CMS\Router\SiteRouter
You can use the $this variable, even if you think you're not within a class.
This can be checked by performing var_dump($this)
There are two methods that can be used.
$this->attachBuildRule(function(){
// build rule code
});
$this->attachParseRule(function(){
// parse rule code
});
If you are using an object implementing RouterInterface the callbacks are the following
$this->attachBuildRule([$myRouter, 'build']);
$this->attachParseRule([$myRouter, 'parse']);
The methods expect a callable and a STAGE, see Router::PROCESS_* constants in namespace Joomla\CMS\Router
If you are using PHPStorm you can write the following code for assistance:
/**
* #var $this Joomla\CMS\Router\SiteRouter
*/
Check out this documentation .
It describes very precisely how to create a Route and how the Routing works in Joomla.
Also check out com_content/router.php as an example

How do I avoid HMVC design pattern in Laravel?

So I have been reading through From Apprentice to Artisan by Taylor Otwell, Laravel Author
And I came across this 'mantra' : HMVC usually indicates poor design.
Which is kind of true...
Also Taylor has suggested
Feel the need to call controllers from other controllers? This is often indicative of poor
application design and too much business logic in your controllers. Extract the logic into a
third class that can be injected into any controller.
And I don't seem to find such way yet..
How can I avoid HMVC and extract the logici nto a third class that can be injected to any controller?
I came up with a neat way to do it, and it seems to have helped me speed up my workflow...
I think this alternative I made can replace HMVC, as well as the conventional way of using controllers... as now controllers are but somehwere where our -what I called- 'motors' are injected.
Check out my article at coderwall where I went through the whole thing.
Read through it, and hopefully it will provide a better way of doing things, starting from the models to and finishing at controllers.
However if you wish to proceed your own way, make sure what was required to be shared between two controllters hiarchecly gets shared in a more neater way, as taylor suggested, shared through injection.
For instance, you are in AdminsController and you feel the need to call an action from UsersController, just make that action and its siblings into a third class, and in your AdminsController
//AdminsController
use ThirdClass;
public function __construct(ThirdClass $mything)
{
$this->myThirdClass = $mything;
}
public function mySharedAction()
{
$this->myThirdClass->mySharedActionFromUsersController();
}
And like so.
Update
If you have gone through my article at coderwall, the one I have mentioned above, I have made a little package that generates all of the mentioned components in there.
Check it out at github

Making a custom class available anywhere using Laravel

In this stackoverflow post,
Get the query executed in Laravel 3/4
Ricardo Rossi provided a great answer about using Kint and a custom class to easily output information about a Laravel query created using the query builder.
I was able to setup Kent using composer but am new to Laravel and haven't used PHP since version 4.
Could someone please provide an example describing how to create a class which can then be called from anywhere. In his example, Ricardo says he uses "DBH::q()".
At the moment, I'm stuck requiring common files just like in good old PHP4 days.
Thanks
You likely want to use psr-0 auto loading with a namespaced class. Here's a post on setting up laravel which says how to do that.
If I understand your question correctly you are asking how to go about using the following syntax DB::q() using your own custom class...
Laravel uses Facades throughout its design which enables you to access classes from anywhere in your app using static style syntax (e.g. Input::get() or Route::get()). I note Fideloper has also provided an answer to your question... He has an excellent tutorial about how to wrap your own custom class in a Facade so you can use this syntax for your own classes and also sidestep the need to inject the dependency in any class that uses it (i.e. once set up correctly it can be called upon anywhere in your app).
Fideloper tutorial is here...
Hope that helps - Good luck

use cakephp component inside a model

How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?
If so, please let me know how I can do so
It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.
A component is thought to share code between controllers, only between controllers.
Components in CakePHP 1.3
Components in CakePHP 2.x
Components in CakePHP 3.x
To share re-usable code between models it would be a behavior. For a view it would be a helper.
If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.
No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.
It is possible to use a component inside a model (but I cannot comment if this is a recommended or a best-practice).
Inspired from original source, an example to use a component called ‘Geocoder’ in a model:
App::import('Component','GeoCoder');
$gc = new GeoCoderComponent(new ComponentCollection);
Then you can use $gc to call the functions of the component.
--
P.S.: I do not want to encourage bad programming practices, but sometimes the pressure of deadlines (in real world projects) may force a developer to make such decisions.
#AD7six
// Use anywhere
AuthComponent::user('id')
// From inside a controller
$this->Auth->user('id');
From the cake PHP documentation they provide AuthComponent::user('id') so that it can be used in places other than a controller.
Maybe I need a bigger hint, but why shouldn't my model be able to access ACL information ?

Creating a RESTful application in Zend

I have a controller that extends Zend_Controller_Action. It contains some actions that I need to give people access to via a RESTful MVC web service.
I've seen some articles that have told me to extend using a different class (Zend_Rest_Controller) but this seems to mean I need to override certain abstract methods and really I no use for most of them (I have my own functions that are quite specific!).
I've seen some code that I've meant to copy into my bootstrap.php and makes use of the FrontController. However, everything I've seen is awfully documented.
Can anybody give me an example that will just work for a controller called, say 'catalog' that contains two actions 'getRoot' and 'checkLatest'? (It should be simple yet I can't get anything to work and have a deadline tomorrow!)
Or else, perhaps point me in the right direction... (I've no idea how to troubleshoot this and see, for example, what URL I should be using to test or where the route I have setup is directing this... I've been looking at this, btw: http://techchorus.net/create-restful-applications-using-zend-framework)
Thanks a lot! :)
In the article that you mention you have a class:
class ArticleController extends Zend_Rest_Controller
Just like that example you should create a CatalogController. The different methods that you require should be sent as parameters HTTP GET parameters. The getAction should perform the operations (depending on your request) and return a collection of of results in the response depending on different possibilities such as "getRoot" or "checkLatest" as you mention.

Categories