Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Sunday, August 28, 2016
How to check php version on xampp? | How to know what version of PHP is used on my xampp?
XAMPP is a popular web server for running PHP script. Sometimes we need to know which version of PHP we used in XAMPP. So in this tutorial I am gonna show how you can check PHP version on XAMPP.
1. Go to your htdocs folder on XAMPP.
2. Create a txt file, write down the following code in it and save it as phpinfo.php .
<?php
phpinfo();
?>
3. Now run the server and access the file from url ( like: localhost/phpinfo.php )
4. You'll see the version of PHP in the webpage.
4. You'll see the version of PHP in the webpage.
Thursday, June 23, 2016
What is the difference Between Cache and Session | Cache Vs Session in PHP
In a recent project I had to use Session and Cache Both. But I wasn't aware of what's the major difference in Session as well as Cache. Because in that time I used to think as if both are storing some data which we can use further use. Actually the concept is same. But There is some Major differences as well. So Let's see what's that:
Cache can expire after a specific time means we can put some specific amount of time with cache whereas one an Item put into session can stay there forever until the session ends.
Cache is per Application based whereas Session is per User based means session data will be different for the various users for all the various users.
So these are the main difference between Cache an Session. There are other differences is there which will covered soon with example. So till then wait and stay connected with my Blog.
Thank you!
Wednesday, June 15, 2016
Creating a Modular Application in Laravel | Modul Structure in Laravel
What is Moduler Application why we use?
Modular application means dividing all your code in Different Module. In those Module are having Controller, Route, View, Model, Helper etc. just like your fresh laravel application installation file.
See the following Diagram How it organized:
The basic idea or importance of using this module architecture is diving the giant code into small meaning modules so that it helps to use your project more manageable and enjoyable.
Every Module contains Controller, Route, View, Model, Helper etc. So we can create number of modules in our application. There might be several ways to approach this, But I have used the following approach. Let's start ..
1. First we need to install this package in your laravel Project : Artem-Schander/L5Modular
It's very easy process. If you can't don't hesitate to mention in comment. I will tell the process
2. Create a directory called Modules inside app directory. This directory will have separate folder for each of the modules. For instance there can be folder called User, one called Company etc. Let's create a folder inside this Modules folder name Course.
3. Now Let's create another three directory called Controllers, Views, Models and a file called routes.php inside that App\Modules\Course.
Actually there is nothing special in Module. Means routes.php is like normal routes.php file as we used in normal architecture in Laravel. What we need to focus mainly is namespacing. We have to give proper namespacing to each controller/model that we created.
4. So final directory structure may be looked like the following Diagram :
2. Create a directory called Modules inside app directory. This directory will have separate folder for each of the modules. For instance there can be folder called User, one called Company etc. Let's create a folder inside this Modules folder name Course.
3. Now Let's create another three directory called Controllers, Views, Models and a file called routes.php inside that App\Modules\Course.
Actually there is nothing special in Module. Means routes.php is like normal routes.php file as we used in normal architecture in Laravel. What we need to focus mainly is namespacing. We have to give proper namespacing to each controller/model that we created.
4. So final directory structure may be looked like the following Diagram :
app\
Modules\
Course\
Controllers\
Models\
Views\
routes.php
5. Now make Controller called CourseController.php inside the Controllers folder. Make a Model called Course.php. and Make a view page called courseSavePage.blade.php where I will load the form to store course data.
(Just check out whether your application's .env file properly configured with database otherwise application will not run properly).
6. Here I'm posting what I wrote in CourseController.php, Course.php, courseSavePage.blade.php, routes.php . What you need to focus is how i maintain the namespacing in those file. If you got the whole namespacing which i have used on those it will be easy for you to understand how actually that modular structure works.
First Course.php:
<?php
namespace App\Modules\Course\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $table='courses';
protected $fillable = ['id','name','code'];
}
Secondly CourseController.php:
<?php
namespace App\Modules\Course\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Course\Models\Course;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
class CourseController extends Controller
{
public function getcourseSavePage()
{
return view('Course::courseSavePage');
}
public function postcourseSave(Request $request)
{
$this->validate($request,[
'code' => 'required|unique:courses|max:5',
'name' => 'required|unique:courses'
]);
$course = new Course();
$course->code = $request->Input(['code']);
$course->name = $request->Input(['name']);
$course->save();
return redirect('courseSavePage');
}
}
Third one is courseSavePage.blade.php:
<html>
<head>
<title> Test Module</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container" >
<h3> Create Course </h3>
{!! Form::open(array('route' => 'saveCourse','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<div class="form-group">
<label>Code</label>
<input type="text" name="code" class="form-control" required placeholder="Code">
</div>
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" required placeholder="Name">
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
</body>
</html>
Fourth one is the routes.php:
<?php
Route::group(array('module' => 'Course','middleware' => 'web','namespace' => 'App\Modules\Course\Controllers'), function() {
Route::get('/courseSavePage',[
'uses'=>'CourseController@getcourseSavePage',
'as'=>'courseSavePage'
]);
Route::post('/saveCourse',[
'uses'=>'CourseController@postcourseSave',
'as'=>'saveCourse'
]);
});
7. So That's it if you access the url /courseSavePage program will load the blade view page and you can store the required data by using other route /saveCourse
That's it today. Hope you enjoy it and implement in your recent project. It will not only make your project manageable but also make it structured. So try to get used to it.
Thank you and stay tune for more next tutorial.
Sunday, June 5, 2016
Phone Number Validation in Laravel | How to Validate Phone Number in Laravel?
In this tutorial I'm gonna show how you can validate user phone number using Laravel Validation Request. As we know usually we write the validation request in Controller. So Let's define a controller where we will show the validation rule.
Controller:
public function saveUser(Request $request){
$this->validate($request,[
'name' => 'required|max:120',
'email' => 'required|email|unique:users',
//'phone' => 'required|numeric|size:11'
'phone' => 'required|regex:/(01)[0-9]{9}/'
]);
$user = new User();
$user->name= $request->Input(['name']);
$user->email= $request->Input(['email']);
$user->phone= $request->Input(['phone']);
$user->save();
return redirect('success');
}
As we can see here I have used two validation rule which one you prefer use that one.
//'phone' => 'required|numeric|size:11'
'phone' => 'required|regex:/(01)[0-9]{9}/'
First one I have used for checking phone number. As Phone number should be numeric I have used numeric and it should be 11.
In Second one I have used Regular Expression for validation. Where Phone number should be 11 digit where it starts with 01 and following 9 digit would as regard from any any digit from 0 to 9.
Here I have shown two process of Phone number validation. You can also create your own validation rule.
That's it for today. Stay connected for the next session.
Thank you!
Saturday, June 4, 2016
PHP Laravel : Important Artisan Command
1. For Generating Migrated Database.It will automatically generate User table in databaseafter fresh installation of Laravel
php artisan migrate
2. For Installing composer in the required project or the project you have downloaded from the git repository.
composer install
3. For Updating Composer in Laravel Project. (Mostly required when we install a new package in existing project)
composer update
4. For Creating table in database. (Here I'm creating option table).
php artisan make:migration create_option_table
5. For generating unique key/ product key in your laravel project.
php artisan key:generate
6. For making New Controller in your Laravel Project. (Here I'm making TodoController)
php artisan make:controller TodoController
7. For Creating New Model in your project.( Here I'm creating Task Model )
php artisan make:model Task
8. For Creating Model as well as Table. ( Here I'm creating Task Model and tasks table with the below command)
php artisan make:model Task -m
9. Laravel provides a quick way to generate all of the routes and views you need for authentication by below command:
php artisan make:auth
10. For showing all the artisan command
php artisan
11. For listing out the routes you have used in your project:
php artisan route:list
12. For createting Event and listener we can use following command:
php artisan event:generate
Friday, June 3, 2016
How to Make a PDF from View in Laravel? | How to generate pdf using blade view with dompdf in laravel?
In this session we gonna a make a very cool thing. Sometimes we need to generate Pdf for creating report or to show data into document . So today we are gonna convert a static view page to pdf using Laravel.
1. In the first process we gonna install a package called barryvdh/laravel-dompdf
It's very easy to install in your project. Just go through the doc and have installed it.
If you can't install in your project, Just ping me in comment.
2. Then create a route in Route.php
Route::get('/pdf', [
'uses' => 'courseController@createPdf',
'as' => 'pdf'
]);
3. Then create a Controller :
public function createPdf()
{
$alldata=Course::all(); // it'll load all the data from course model.
$pdf = \PDF::loadView(
'invoice', compact('alldata'))->setPaper('a4')->setOrientation('portrait');
return $pdf->download('invoice.pdf');
}
And don't forget to use namespace above the the controller otherwise it will show Error.
namespace we have to use:
use Barryvdh\DomPDF\Facade as PDF;
4. In controller you save seen that in that loadView('invoice') (It's actually a view page which will generate the PDF). So we have to create first this view page where we can show all the data what a user wanted to see.
5. That's it so when the user click on that pdf route, it will automatically generate/ convert the page into PDF.
That's it for today. Hope you liked it. If you want me to post more tutorial on laravel don't forget to comment or feedback.
Thank you.
Thursday, June 2, 2016
How to remove /public/ from URL in Laravel Project?
In this instructional exercise we will figure out how to remove public from laravel url.You can utilize this technique for laravel5.X+ and lower versions.By default, the URL which accompanies Laravel App will be this http://example.com/pubic/.So to get perfect URLs which are useful for SEO reason you should remove public from url in laravel application.
It's very easy for laravel 5+ .Just do the following process:
1. Rename the server.php in the your Laravel root folder to index.php
2. Copy the .htaccess file from /public directory to your Laravel root folder.
That's it. Just Reload the Project. You will see the project url without /public in it.
Let's see with a demo video Tutorial.
Wednesday, June 1, 2016
PHP Laravel : How to logout a user from all devices using Redis?
Here is a common scenario like after password reset I/Admin want to logout the user from all device except the device which he logged in while password reset or suppose a user have logged in any many device and the admin want to logout from all of the devices for security purpose for certain reason.
In laravel we can finish the task very easy way using Redis. Lets solve the issue with the following steps.
1. First install the Predis package by composer:
In composer.json file add the following line and run update composer in your cmd.
"require": {
......
"predis/predis": "~1.0" ,
......
},
2. Create a Session table in database using the following commands:
2. Create a Session table in database using the following commands:
php artisan session:table
composer dump-autoload
php artisan migrate
3. Then Download Redis in your PC from the below link and install it in your C:// drive in program files folder.
4. After Redis installed run redis-server.exe file means your server is ready. Now if you run your application.
5. Don't forget to change your .env setting for following driver :
CACHE_DRIVER=redis
SESSION_DRIVER=redis
5. Here is a sample controller code I have shared for logged in using Redis as well as logged out.
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request['email'], 'password' =>$request['password'] ]) ) {
//for save session ID I use (when user login )
$redis = \Redis::connection();
$userId = Auth::user()->id;
$redis->sadd('users:sessions:' . $userId,Session::getId());
return redirect()->route('main');
}
return redirect()->back();
}
public function getLogout()
{
$redis = \Redis::connection();
$userId=Auth::user()->id;
//get all session IDs for user
$userSessions = $redis->smembers('users:sessions:' . $userId);
$currentSession = Session::getId();
//for logout from all devices use loop
foreach ($userSessions as $sessionId) {
if ($currentSession == $sessionId) {
continue;
}
//for remove sessions ID from array of user sessions (if user logout or manually logout )
$redis->srem('users:sessions:' . $userId, $sessionId);
//remove Laravel session (logout user from other device)
$redis->del('laravel:' . $sessionId);
}
Auth::logout();
return redirect()->route('main');
}
6. That's it! When a user logged out from a particular device it will automatically kill all the sessions and logged out from other device also.









