Tuesday, 18 October 2016

Enabling Other Computer to Access Wamp server in a Windows PC in same Network

When you work for a large project, their will be situation comes where you have to share your wamp server with other persons on the same networks, especially when the project is not hosted else where.
For Linux Users it wont be a problem beacause, by default this is enabled in linux systems. You can access from other pcs of a Linux wamp server at any time. Just browse by :- http://ipaddress/

So In case of Windows to enable this facility, we have to do some configuration in our wamp settings. So am going to explain what to do for this:-



For WampServer 2.5 and previous versions

WAMPServer is designed to be a single seat developers tool. Apache is therefore configure by default to only allow access from the PC running the server i.e. localhost or 127.0.0.1 or ::1
But as it is a full version of Apache all you need is a little knowledge of the server you are using.
The simple ( hammer to crack a nut ) way is to use the 'Put Online' wampmanager menu option.
left click wampmanager icon -> Put Online
This however tells Apache it can accept connections from any ip address in the universe. That's not a problem as long as you have not port forwarded port 80 on your router, or never ever will attempt to in the future.
The more sensible way is to edit the httpd.conf file ( again using the wampmanager menu's ) and change the Apache access security manually.
left click wampmanager icon -> Apache -> httpd.conf
This launches the httpd.conf file in notepad.
Look for this section of this file
<Directory "d:/wamp/www">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
#    Require all granted
#   onlineoffline tag - don't remove
     Order Deny,Allow
     Deny from all
     Allow from 127.0.0.1
     Allow from ::1
     Allow from localhost
</Directory>
Now assuming your local network subnet uses the address range 192.168.0.?
Add this line after Allow from localhost
Allow from 192.168.0
This will tell Apache that it is allowed to be accessed from any ip address on that subnet. Of course you will need to check that your router is set to use the 192.168.0 range.
This is simply done by entering this command from a command window ipconfig and looking at the line labeled IPv4 Address. you then use the first 3 sections of the address you see in there.
For example if yours looked like this:-
IPv4 Address. . . . . . . . . . . : 192.168.2.11
You would use
Allow from 192.168.2
UPDATE for Apache 2.4 users
Of course if you are using Apache 2.4 the syntax for this has changed.
You should replace ALL of this section :
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from ::1
Allow from localhost
With this, using the new Apache 2.4 syntax
Require local
Require ip 192.168.0
You should not just add this into httpd.conf it must be a replace.

For WAMPServer 3 and above

In WAMPSevrer 3 there is a Virtual Host defined by default. Therefore the above suggestions do not work.
Instead, leave the server OFFLINE as this funtionality is defunct and no longer works, which is why the Online/Offline menu has become optional and turned off by default.
Now you should edit the \wamp\bin\apache\apache{version}\conf\extra\httpd-vhosts.conf file. In WAMPServer3.6 there is actually a menu that will open this file in your editor
left click wampmanager -> Apache -> httpd-vhost.conf
just like the one that has always existsed that edits your httpd.conf file.
It should look like this if you have not added any of your own Virtual Hosts
#
# Virtual Hosts
#

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot c:/wamp/www
    <Directory  "c:/wamp/www/">
        Options +Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>
Now simply change the Require parameter to suite your needs EG
If you want to allow access from anywhere replace Require local with
Require all granted
If you want to be more specific and secure and only allow ip addresses within your subnet add access rights like this to allow any PC in your subnet
Require local
Require ip 192.168.1
Or to be even more specific
Require local
Require ip 192.168.1.100
Require ip 192.168.1.101

Thursday, 13 October 2016

Laravel Sample Project for Beginners with CRUD Functions

Starting The Project

Download Sample project from :-
 Download: https://github.com/litto/Laravel5--Sample-Project--Including-CRUD-Functions

In this blog Iam going to explain a Sample project with all functions that is needed like Creating, Updating, Deletion,File Uploading, Routing, Session management.etc.. Hope you hav installed laravel project as I explained in my previous post. If not read it :-

http://phpdudes.blogspot.ae/2016/10/laravel-5-installation-steps-for.html


Creating a Database

1) First Create a db of your preffered name,Iam naming it as Laravel.
2 create tables cms, admin, cms_userauth.
3) cms_admin contain fields like auth_id,username, password.
4) cms_userauth contain fields like user_id,name,email,password

Connecting DB to the project
1) Find a file name .env in the root folder of your project
2) Find the lines

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=newlaravel
DB_USERNAME=root
DB_PASSWORD=

3) Give values for each variables.

4) Your db connection has been established

Folder Structure

This will be your project folder structure :-


I will explain you, purpose of each folder one by one

app − This directory contains the core code of the application.

bootstrap − This directory contains the application bootstrapping script.

config − This directory contains configuration files of application.

database − This folder contains your database migration and seeds.

public − This is the application’s document root. It starts the Laravel application. It also contains the assets of the application like JavaScript, CSS, Images, etc.

resources − This directory contains raw assets such as the LESS & Sass files, localization and language files, and Templates that are rendered as HTML.

storage − This directory contains App storage, like file uploads etc. Framework storage (cache), and application-generated logs.

test − This directory contains various test cases.

vendor − This directory contains composer dependencies.


Start The Project

Routing

Now the important Feature of our project is Routing. It is like explaining which controller & function each url should use.
So, you can find the route configuration file in /app/Http/ folder. Here opne the file named routes.php.

In my I have already defined my routes:-

For eg:

Route::get('/index', array('as' => 'index', 'uses' => 'AdminController@index'));

This means If a request comes like: http://localhost:8000/index . Then this route condition will triggered.
It will use AdminController & function index defined in the controller.

Route::get('/list', array('as' => 'list', 'uses' => 'UserController@index'));

Here If request like http://localhost:8000/list comes then it will use UserController & Function index defined in it.
Now may be you are wondering what is this Controllers & functions & all. I will explaain in the coming portions.

So see another Route line like this:

Route::get('edit/{id}','UserController@edit');

This is for urls like : http://localhost:8000/edit/7
Here the Id will be passed as variable to the Function

Defining Controllers & Views

We will define controllers in the folder /app/ Http/Controllers/
Here For eg: If you need to create a Controller for  User table. Create controller with name: UserController.php

You can see in My project UserController.php

In it I will explain one Function:-
Lets take index function defined their:-

   public function index(){
     $users = DB::table('cms_userauth')->get();
      return view('user_list',['users'=>$users]);
   }

   Here the database functions I will explain later, see the line

    return view('user_list',['users'=>$users]);

   Here when route calls this Controller & function, what it is  doing is that, it will fetch all user details & it is returing a view file named user_list & its passing an array to that file.

   Defining Views

   In previous Function it is calling a view file right? Actually It is the view for users.
   This is defined in /resources/views/ folder.
   Here in my project you can see user_list.blade.php
   This File willl be displayed.. For knowing about why blade.php extension comes. Actually It is a laravel templating function. For more details about blade template check in Laravel Site.

Installing Html Package

Laravel provides various in built tags to handle HTML forms easily and securely. All the major elements of HTML are generated using Laravel. To support this, we need to add HTML package to Laravel using composer.

Step 1 − Execute the following command to proceed with the same.

composer require illuminate/html

 Now, we need to add this package to Laravel configuration file which is stored at config/app.php. Open this file and you will see a list of Laravel service providers as shown in the following image.


 Step 4 − Add aliases in the same file for HTML and Form. Notice the two lines indicated in the outlined box in the following image and add those two lines.


But there is a bug in this package, I dont know why it is like that. But you can solve taht issue like :-
Navigate to vendors/illuminate/html/HtmlServiceProvider.php
Here change  the lines 36 & 49.
$this->app->bindShared('html', function($app)   to  $this->app->singleton('html', function($app)
$this->app->bindShared('form', function($app)   to  $this->app->singleton('form', function($app)
Changed File will be like this:-



Now Package problem will be solved

Including Css in Views

Now using this Html package templating the method of including css is slightly different. Place your css inside public folder in root.
create a folder named css & include all your css files their.

Now include it in your view file like this:-
 
{!! HTML::style('css/bootstrap.min.css') !!}

Here boostrap.min.css is placed inside css folder


Including Js File in Views

Now using this Html package templating the method of including Js is slightly different. Place your Js inside public folder in root.
create a folder named Js & include all your Js files their.

Now include js file like this:-

{!! HTML::script('js/bootstrap-datepicker.js') !!}

Here js/bootstrap-datepicker.js is placed in the js folder in public.


Including header/ footer Files in Your Views

So In your project mostly we have to include same files in view files like header, footer, menu.etc.. So in Laravel the method is slighly different.
For eg: you have header.blade.php, footer.blade.php & user_list.blade.php in your views folder.
Then to include header & footer in the user_list.blade.php file, You just code like this:-

@include('header')

---------code here------------

@include('footer')

For more clarity check my project included in this post.


Displaying Records in View

So In our Controller , index function we passed a array named users to user_list view right?
So we are displaying in the view like this:-

    @foreach ($users as $user)    
{{ $user->user_id }}  //id displayed
{{ $user->email }} // mail will be displayed

 @endforeach



Creating Forms

Open forms like this
<?php   echo Form::open(array('url' => '/createsubmit','files'=>'true')); ?>

as we have installed Html package, it will defaulty provide form functions like this:-
Just refer in  my project folder & look into each file you will understand clearly.


Create Records in DB (with File Uploading)

While defining Create Submit Form Function dont forget to pass a request parameter in the function definition:-

    public function createsubmit(Request $request){

        $name = $request->input('name');
        $email = $request->input('email');
        $file = $request->file('userfile');
        $destinationPath = 'uploads';
        $file->move($destinationPath,$file->getClientOriginalName());
        $imagename= $file->getClientOriginalName();
if($imagename==''){
$imagename='user.png';
}
DB::table('cms_userauth')->insert(
    array('name' =>$name, 'email' => $email,'photo'=>$imagename,'status'=>1)
);

      echo "Record inserted successfully.<br/>";
      return redirect('list');
   }

Here we can get value of each variable submitted using:-
 $request->input('variable name');  this function

 Image will be uploaded to our defined destination like:-
   $file->move($destinationPath,$file->getClientOriginalName());
   This will upload to the folder uploads we created in public folder..
   File can be viewed by:- <image  height="100px" width="100px" src="/uploads/{{ $user->photo }}"/>
Then  data is inserted in the table cms_userauth as:-

DB::table('cms_userauth')->insert(
    array('name' =>$name, 'email' => $email,'photo'=>$imagename,'status'=>1)
);



Updating Data in DB

DB::table('cms_userauth')->where('user_id',$id)->update(array('name' =>$name, 'email' => $email,'photo'=>$imagename));


Fetching Single Record

$users = DB::table('cms_userauth')->where('user_id', '=', $id)->get();

Fetching all records

  $users = DB::table('cms_userauth')->get();

Fetching records with Condition

$users = DB::table('cms_userauth')
                    ->where('votes', '>', 100)
                    ->orWhere('name', 'John')
                    ->get();

Deleting records

DB::table('cms_userauth')->where('user_id', '=', $id)->delete();

For more Information on DB Queries ..please refer:- https://laravel.com/docs/4.2/queries

Redirecting to another view

For redirecting to one function from other
 return redirect('index');


Sessions in Laravel

For setting a session variable, save like this:

$request->session()->put('loginadminname', $users[0]->username);


For getting all sessions

$data = $request->session()->all();

For deleting all session variables:-

$request->session()->flush();


Now I think u got a little idea about laravel... So for more clarification download my project & go through this post..


Download: https://github.com/litto/Laravel5--Sample-Project--Including-CRUD-Functions



Laravel 5 Installation Steps for Windows & Linux

What is Laravel?

Laravel is a powerful MVC PHP framework, designed for developers who need a simple and elegant toolkit to create full-featured web applications. Laravel was created by Taylor Otwell.

Laravel is a MVC framework with bundles, migrations, and Artisan CLI. Laravel offers a robust set of tools and an application architecture that incorporates many of the best features of frameworks like CodeIgniter, Yii, ASP.NET MVC, Ruby on Rails, Sinatra, and others.

Laravel is an Open Source framework. It has a very rich set of features which will boost the speed of Web Development. If you familiar with Core PHP and Advanced PHP, Laravel will make your task easier. It will save a lot time if you are planning to develop a website from scratch. Not only that, the website built in Laravel is also secure. It prevents the various attacks that can take place on websites.

Installing Laravel (Windows)

1) First step is to install composer in your system. If composer is not present download it from https://getcomposer.org/download/

2) After Composer Installtion, open command prompt & type composer & press enter.


3) We are working on MYsql database . So for that I am expecting your system has wamp server installed & settedup.So For easy development I will explain you steps one by one as i did.

4) In the wamp/www directory, Create a folder as your wish.

5)In Command prompt navigate to that folder and then run command like this:-

composer create-project laravel/laravel

6) So you its will run in terminal & automatically larvel project is generated in laravel folder inside the directory you created.

7)Now Run  this command

php artisan serve

8) Now the terminal will tell you like your production is started at http://localhost:8000/

9) Navigate to that URL through browser.

10) You will get like this :- as shown in below image. Congrtazz your Laravel has been Installed..


Installing Laravel in (Linux)

1) Open Terminal & Run command like

sudo curl -s https://getcomposer.org/installer | /opt/lampp/bin/php

2) This will Install composer in the system.

3) Now run this command in terminal

sudo  mv composer.phar /usr/local/bin/composer  

4)Then to check composer is working or not.. just type composer in terminal.It will show composer commands. So that's working.

5) Then create a folder with anyname eg: laravelworks anywhere in you system . then go to that folder from terminal by using cd command .

6)  Inside that folder from terminal give a command like this .

composer create-project laravel/laravel your-project-name 

7)Next thing we need to do is give permissions to some folders .

i) storage directory
ii) bootstrap/cache

8) you can do that in any way as you like ... i did like this .

first go inside  –prefer-dist using cd command

sudo chmod 777 -R ./storage
sudo chmod 777 -R ./bootstrap/cache


9)After that we need to start a laravel server using following command

php artisan serve

10)   Now the terminal will tell you like your production is started at http://localhost:8000/.
 Congrtazz your Laravel has been Installed..

Thursday, 6 October 2016

Setting Cron Jobs & Folder Sharing in Ubuntu

Commands to set cronjobs in Ubuntu

Login Via root in ubuntu
sudo su
Give Password
type crontab -e

Now a tab will appear, their give crontab line like:-

eg: 30 23 * * * wget http://localhost/crm/yourscript.php
for running a script at  11.30 pm


Folder Sharing

The Procedure is we have to create a user, & group. Then add that user to a group , then at last give permission to the folder for this usergroup.. The commands will be explained with an example below:-

1) Create a User named for eg; developer
adduser developer
smbpasswd -a developer
smbpasswd  -e developer
Give password


2)Create a group named developers

sudo addgroup developers

3) Add User to Group

sudo adduser developer developers

4) Give permission to that folder to the user group

chown -R developer:developers /var/www/foldername

5) Now we have to check whether we installed samba in the computer
Try running this code , If its alreday installed then no pblm.. otherwise install.

sudo apt-get install samba
6) Now in the terminal type like this:-

gksudo gedit /etc/samba/smb.conf

7) Now a file will be opened, in it after the [printer] definition include some lines like this:-

[foldername]
       
        comment = share
        browseable = yes
        path = /var/www/html
       printable= no
       guest ok = yes
       readonly = no
       writable = yes
       valid users = @developers
       write list = developer
      create mask = 0775
      directory mask = 0775