Built in global helper functions were removed by default in v2.1 though they are not deprecated and you can use them as you wish.
Masonite works on getting rid of all those mundane tasks that developers either dread writing or dread writing over and over again. Because of this, Masonite has several helper functions that allows you to quickly write the code you want to write without worrying about imports or retrieving things from the Service Container. Many things inside the Service Container are simply retrieved using several functions that Masonite sets as builtin functions which we call "Built in Helper Functions" which you may see them referred to as.
These functions do not require any imports and are simply just available which is similiar to the print()
function. These functions are all set inside the HelpersProvider
Service Provider.
You can continue to use these helper functions as much as you like but most developers use these to quickly mock things up and then come back to refactor later.
It may make more sense if we take a peak at this Service Provider:
Notice how we simply just add builtin functions via this provider.
The below list of helpers are "builtin" helpers meaning they are global in the same way that the print
method is global. These helpers can be used without any imports.
The Request class has a simple request()
helper function.
is exactly the same as:
Notice we didn't import anything at the top of the file, nor did we inject anything from the Service Container.
The view()
function is just a shortcut to the View
class.
is exactly the same as:
Instead of resolving the mail class you can use the mail helper:
is exactly the same as:
The auth()
function is a shortcut around getting the current user. We can retrieve the user like so:
is exactly the same as:
This will return None
if there is no user so in a real world application this may look something like:
This is because you can't call the .id
attribute on None
We can get the container by using the container()
function
is exactly the same as:
We may need to get some environment variables inside our controller or other parts of our application. For this we can use the env()
function.
is exactly the same as:
We can resolve anything from the container by using this resolve()
function.
is exactly the same as:
That's it! These are simply just functions that are added to Python's builtin functions.
Die and dump is a common way to debug objects in PHP and other programming languages. Laravel has the concept of dd() which dies and dumps the object you need to inspect.
dd()
is essentially adding a break point in your code which dumps the properties of an object to your browser.
For example we can die and dump the user we find:
You may also specify several parameters for see several values dumped at once:
If we then go to the browser and visit this URL as normal then we can now see the object fully inspected which will kill the script wherever it is in place and throw an exception but instead of showing the normal debugger it will use a custom exception handler and show the inspection of the object instead:
There are several helper methods that require you to import them in order to use them. These helpers are not global like the previous helpers.
The config helper is used to get values in the config directory. For example in order to get the location in the config/storage.py
file for example.
This function can be used to retrieve values from any configuration file but we will use the config/storage.py
file as an example.
With a config/storage.py
file like this:
We can get the value of the west key in the location inner dictionary like so:
Instead of importing the dictionary itself:
Note the use of the lowercase storage.drivers.s3
instead of storage.DRIVERS.s3
. Either or would work because the config function is uppercase and lowercase insensitive.
This helper that allows you to wrap any object in this helper and call attributes or methods on it even if they don't exist. If they exist then it will return the method, if it doesn't exist it will return None
.
Take this example where we would normally write:
We can now use this code snippet instead:
Compact is a really nice helper that allows you to stop making those really repetitive dictionary statements in your controller methods
take this for example:
Notice how our Python variables are exactly the same as what we want our variables to be in our template.
With the compact function, now you can do:
You can also pass in a dictionary which will update accordingly:
You can use the same Collection class that ORM uses when returning model collections. This can be used like so:
You have access to all the methods on a normal collection object.
Masonite uses this helper internally but if you find a need to parse query strings in the same way Masonite does then you can use this helper like this: