v1g1lance.netlify.app
Laravel Lumen Generate App Key
- Laravel Lumen Generate App Key In Windows 10
- Laravel App Function
- Generate App Key In Lumen
- Laravel Lumen Generate App Keyboard
- Laravel Lumen Generate App Key Fob
- Apr 27, 2017 n the first installment of the series on creating a ToDo App with Laravel 5.4, I added an APIKEY column in the user table and commented that I might use it in A Lumen REST API authentication is a great way of accessing a PHP app. Read this article on how to create and test (using PostMan) a Lumen powered REST API.
- Jul 27, 2015 Lumen packages are a great way to incorporate some pre-made functionality into your app. They work just like Laravel packages, but when creating one for Lumen, one needs to change a couple things to make it work. Luckily it's quite simple to update a package to be used between Laravel and Lumen. Here we'll create a Lumen package from scratch.
I’m trying out the PHP micro Framework Lumen (from Laravel).
Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON. Generating Resources. To generate a resource class, you may use the make:resource Artisan command. By default, resources will be placed in the app/Http/Resources directory of your application.
One of my first steps was to look into the .env.example
file and make a copy of it to have my .env
file. There is a variable APP_KEY just like there is in Laravel. Now I tried out the simple command php artisan key:generate
to get my new key But I ran into the following error message
[InvalidArgumentException]There are no commands defined in the 'key' namespace.
Does some one know how I can generate keys for Lumen?
Update with solution
So I found my favorite solution for this problem. On the command line (Linux) I run php -r 'echo md5(uniqid()).'n';'
what gives me something like this 7142720170cef01171fd4af26ef17c93
.
If you are going to use Lumen more often, you may want to create an alias in your .bashrc
, which is located in your home directory /home/USERNAME
. To do so, you can open the file with nano ~/.bashrc
or vi ~/.bashrc
and copy the following alias at the end of the file, alias phpkey='php -r 'echo md5(uniqid()).'n';'
. Now you can use the command phpkey
which will give you a 32 character long random string 🙂
Laravel Lumen Generate App Key In Windows 10
The Laravel command is fairly simple. It just generates a random 32 character long string. You can do the same in Lumen. Just temporarily add a route like this:
Then go to /key
in your browser and copy paste the key into your .env
file.
Afterwards remove the route.
Obviously you could also use some random string generator online. Like this one
Firstly, you have to register your key generator command, put this Lumen Key Generator Commands to app/Console/Commands/KeyGenerateCommand.php
. To make this command available in artisan
, change appConsoleKernel.php
:
After that, configure your application so that IlluminateConfigRepository
instance has app.key
value. To do this, change bootstrap/app.php
:
After that, copy your .env.example
file to .env
:
Ignore this step if you already use .env
file.
Enjoy you key:generate
command via:
Edit
You may use Lumen Generator. It covers so much commands you are missing from Laravel.
An easy solution is just running PHP code from the terminal (without using tinker
, because that is not available with Lumen):
It uses Laravel’s Str::random()
function that makes use of the secure random_bytes()
function.
The APP_KEY generation is a step of development process (I don’t think that creating temporarily routes is a practical way to do it). The function str_random
can help us, but this function is part of Laravel/Lunmen framework.
I recommend running tinker
php artisan tinker
and then run the function
>>> str_random(32)
The result is the key you’re looking for.
=> 'y3DLxnEczGWGN4CKUdk1S5GbMumU2dfH'
For me the easiest way to generate a Lumen key is typing on console one of these commands:
or
openssl rand -base64 24
depending of your environment. In my case, I aways use date md5
on mac
This answer was inspired by @thomas-venturini ‘s update to the question. Here’s a bash script that takes care of creating .env
and updating it with an APP_KEY
using the aforementioned PHP command and the UNIX sed
command:
Hope someone finds this useful.
Tags: phpphp
iCal, short for iCalendar, is an internet standard file format used to store calendar information. Being a standard format, it is compatible with most online calendars, giving you access to important dates regardless of your preferred client (Google Calendar, Outlook Calendar, Apple Calendar, etc.). Even popular online services use the iCal format to help their users remember important dates. Airbnb, for example, uses the iCal format to store room availability giving users the ability to export their Airbnb calendar and view it on an external calendar.
In this tutorial, you’ll learn about the iCal format and how to create an iCal calendar feed using Lumen, a PHP micro-framework by Laravel that allows you to quickly build elegant APIs.
Tutorial Requirements
For this tutorial, you will need:
- A PHP development environment
- A global installation of Composer
- A global installation of ngrok
- A PostgreSQL Database
The iCal Object
Below is a sample iCal object:
The iCal object has three parts: begin, body and end. The iCal object must start with BEGIN:VCALENDAR
and end with END:VCALENDAR
. The body consists of a sequence of properties and one or more calendar components.
In our example above, we have two properties, VERSION
and PRODID
. While there are many other properties, these two must be present in an iCal object in order for it to parse correctly.
PRODID
is your company details in the format: Business Name//Product Name//Language. VERSION
is the current version of iCal.
A calendar can have multiple components, each grouped inside a begin and end delimiter. A component is a collection of properties that express a particular calendar semantic. For example, the calendar component can specify an event, to-do, journal entry, time zone information, free/busy time information or an alarm. In our example above, the event properties are grouped inside the BEGIN:VEVENT
and END:VEVENT
delimiters.
Here is some information on a few properties of an event component:
- UID: A unique ID for the event (required).
- DTSTAMP: The date the event was created (required).
- DTSTART/DTEND: The start and end timestamps of an event in UTC format.
- SUMMARY: The event title.
For more information on the iCal object and its properties, please check out the official documentation.
Setup
Starting a Lumen Project
Lumen is a fast PHP micro-framework by Laravel. This micro-framework makes it very easy to bootstrap a new project with the ability to handle up to 1900 requests per second.
To start a new lumen project via Composer, run the following command:
The last part of the command is the name of our project. I have named mine 'iCal'. Once we run the command, Composer downloads all required dependencies and prepares our application.
Let’s check if everything worked as expected. We will start our application by running the command:
Our application is now served on localhost port 8000. On your browser, navigate to http://localhost:8000/. You should see the version of lumen you’re using. At the time of writing this tutorial, it is version 5.7.4.
Database and Environment Configuration
The next step is to create a database and configure our connection to the database. Lumen supports four database systems: MySQL, PostgreSQL, SQLite and SQL Server. I am using PostgreSQL for this tutorial. I have created a database called events.
When we created our Lumen application, a .env
file was created for us. Once you have created your database, go ahead and update the database credentials in the .env file. Here is what your updated .env should look like:
That is everything we need to do to connect our database. We’re making good progress, however, we have no way of testing our database connection just yet. Let’s go ahead and make a migration.
Create a Migration
Migrations allow you to build, modify and share the applications database schema with ease. For our events application, we need one table called tech_events. It should have the following columns:
- id
- name
- starts
- ends
- status
- summary
- location
- uid
- created_at
- Updated_at
To create a migration we will use the make:migration
Artisan command:
The new migration is placed in the database/migrations folder. If you open up the newly created migration, this is what you get:
Inside the up()
method, we create a table and define its columns. The down()
method reverses the migrations if we need to.
Let’s go ahead and update the up() method with the columns we require. This is what we have:
In the above code we have added columns using this format $table->string('name')
. This means we have a column called name and its datatype is a string.
Now that we have defined our schema, let’s do a migration to check if it translates to our database. Run the artisan command:
Check your database for the above migration. I am using Postico to check the database. This is what the database looks like:
Practical breakage was made easier by the quantity of data encrypted with a single key.Thus began the practice of changing keys often: as a 'cleansing operation' to kick out an assumed number of partially successful attackers, and to dance around the inherent weaknesses of the algorithms which were known at that time.But times have changed. We have to go back about one century, when cryptography was a military tool and was based upon a complex network of shared keys and passwords (this was before the invention of asymmetric cryptography). Since a secret which is shared is no longer 'very secret', it was assumed that, in a standing army, the enemy could occasionally learn some keys; also, algorithms of that time were quite weak and could indeed be broken by hand (no computers at that time). Unfortunately, frequent key renewal has all the hallmarks of a procedure meant to appease fearsome deities, as opposed to, say, science.Let's see from where this idea comes from. Dynamically generate encryption keys for each session free. As a general rule, I advise against designing security through the use of voodoo ritual.
Laravel App Function
Great! We have now set up our database, connected it and defined the schema for our tech_events table.
Create the Tech Events Model
We will use Laravel’s Eloquent ORM to interact with our database. To do so we will need to enable the Eloquent ORM by opening the file bootstrap/app.php and uncommenting these lines:
Models are typically inside the app folder. In our app folder, we have a file called User.php. By default, Lumen comes with the User model when we create our application.
If we had many models, a good practice would be to create a folder and store our models in it. Since we are only adding one more model, let us leave it in the app folder.
![Laravel Lumen Generate App Key Laravel Lumen Generate App Key](https://www.cloudways.com/blog/wp-content/uploads/api.jpg)
Create the file app/TechEvents.php and add the following code in it:
We have defined our model. We can now interact with our database using Eloquent. In order to build our calendar feed, we will need some events in our database. Let’s go ahead and seed some data in our database. More on models can be found in the documentation.
Seed Tech Events Data
Lumen provides an easy way to add fake data using factories. We define our model factories in the database/factories/ModelFactory.php file. Let’s have a look at what our file looks like by default:
Generate App Key In Lumen
We can see a model factory is defined for the user model. It has the name and email fields. The Faker library is used to generate fake data for the fields. It is one of the dependencies that was installed when we created the application. You can confirm this by checking the composer.json file under 'require-dev'. Let us update the file by adding a model factory for the tech events model:
We have imported Carbon, a PHP extension for DateTime. We have also declared a variable called $startTime
. This generates a random date within a month from now. For this tutorial, our events will only last two hours. In the 'ends' field we add two hours to the $startTime.
In order to seed our database with fake data, we need one more step. In the file database/seeds/DatabaseSeeder.php, we have one method called run()
. When we run seeders, this is the method that is called.
This method uses model factories to generate and insert data into the database. Let’s add our newly created model factory:
By calling factory(TechEvents::class, 10)->create()
inside the run method, we are indicating that we would like to generate and insert ten records into our database.
It is time to check if we have done everything correctly. Run the command:
Your database should look like this:
We have now successfully added data to our tech_events table.
Building our iCal object
The iCal Controller
We need one controller for this tutorial. In the directory app/Http/Controllers. Create a controller called ICalController.php and add the following code:
In the getEventsICalObject()
method, we start by using Eloquent’s all()
method to fetch all the events from the database and store them in the $events
variable. Secondly, we define the iCal format. This is used to transform our timestamps into UTC format. Then we begin building our iCal object by looping through each event and adding them to the iCal object.
After closing the object we add the required headers. That includes the file name in .ics format. I have named mine 'cal.ics'.
Lastly, we get rid of unwanted spaces which would otherwise make the .ics file invalid.
Creating an Endpoint
So far we have worked on the code that fetches the data and creates the iCal object. Since we still have no way of testing this code, we need to create an endpoint. Inside routes/web.php add this line of code:
We have just declared a route. The method to access this route is GET and the URL to hit the endpoint is http://localhost:8000/ical-events.
Let’s go ahead and test this out on Postman. Make sure your application is running on port 8000. If it isn’t, you can run the command:
Success! We are able to get our events into the iCal format. We need to test this out on different calendars to see if it works. In order to test it on Google and Outlook, we need our application to be accessible through a public URL. This is where ngrok comes in!
While still running the application on localhost, open up a new terminal and run the command:
Test on Google and Outlook
Google Calendar
To test it out on google:
On the left, click the + icon, next to 'Add a friend’s calendar' input box. Select 'From URL' then enter the URL you got from ngrok. Mine is http://e574e220.ngrok.io/ical-events.
Your calendar should now have the events from your database.
Outlook Calendar
To test it on Outlook, on the top menu bar, click “Add Calendar”. Then click “From Internet”, to enter the URL and name your calendar. Click save. That’s it!
Conclusion
All done! We have successfully created an iCal feed and tested it out on Google and Outlook. You can now test it out on different calendars, including your local calendaring application such as Apple Calendar.
Next Steps?
Laravel Lumen Generate App Keyboard
You can create a scheduling application and add the option of downloading an iCal file or giving users a URL to access the iCal feed.
Ruby on rails generate model with foreign key. May 14, 2016 Here, a foreign key of 1 in the categoryid column will relate to food expenses, a foreign key of 2 will relate to accommodation expenses, and so forth. Let's dive in. Generate Models. To start off, I created a new rails application and established the primary database, expenses. In the first command rails generate model Micropost userid:integer it only adds a column userid however rails does not know the use of the col. The keywords belongsto and hasmany determine the relationship between these models and declare userid as a foreign key to User model. I'm quite new to rails and have been trying to work this out all night with no luck. I have created 3 models: users, businesses, and businesshours.I have also added the associations (businesshours belongsto businesses which belongsto users) and (user hasone business which hasmany businesshours).Reading through the docs online it seems I now need to create the foreign keys for.
Also, you can request users to opt into SMS reminders to their calendar using the Twilio SMS API. This tutorial on sending SMS reminders from PHP is a great place to start.
I look forward to hearing about the amazing application you build. You can find the complete code on Github. You can reach me on:
Email: odukjr@gmail.com
Github: charlieoduk
Twitter: @charlieoduk
Happy Coding!