Close

How to implement design or theme in laravel

Laravel use blade template engine. so in all view files we use blade extension for example test.blade.php In this post i am implementing design or theme on my application. Here are some steps to implement design on a web application. here I am working on laravel5 and creating home page

  • so first go to laravel5/app/Http directory and open routes.php and put this code

    Route::get('home', 'HomeController@index');

  • Now go in laravel5/app/Http/Controllers and create a controller HomeController.php if not exist

    namespace App\Http\Controllers; class HomeController extends Controller {

    public function __construct()
    {
        //$this->middleware('auth');
    }
    
    
    public function index()
    {
        return view('home');
    }
    

    }

  • go in laravel5/resources/views and create a file app.blade.php and add html for your header, footer and head for example

    Your title

    @yield('content')

@yield('content') used for fetch content from view

  • put all css js, and images in public folder at root of your application

  • in next step create a file home.blade.php for action 'index' as you can see in index action i used 'return view('home');'

put this code home.blade.php

@extends('app')
@section('content')

    <div>Your content</div>

@endsection

now you app layout will be used in this view

when you will browse it your site url/home

you can see this page with layout. and by Ctrl U you can see all element like footer header and your content. and you can manage its UI by adding css or html in app.blade.php file

hope this will help you

Remove defualt javascript files from head in yii

In yii there are many javascript and css files load by defualt and these can break our custom js or css. if you will check your header code then these will not appear but on load time these comes dynamiclly. we can remove these files by writing some code in our Controller.php under /app/Components/. go to Controller.php file and search init() function if it does not exist create new

public function init(){

}

and now add

public function init(){

    Yii::app()->clientScript->scriptMap=array('jquery.yiiactiveform.js'=>false,'jquery.min.js'=>false);
}

in this code which file you do not want to load make it false like in this example i made jquery.yiiactiveform.js'=>false

How to make url seo friendly in cakephp

In cakephp we can manage our url in routes.php file under /app/Config directory. If you want to create seo friendly url you can define your url in routes.php. In this example i am creating a user profile url in this url we will not show controller and action name we only show username so our will look like example.com/pk

I have controller UsersController and action profile in this case i use url example.com/pk then we can get pk as username in our action

class UsersController extends AppController {

    public function profile($username='') 
    {

        echo $username; die;

    }

}

the out put of this action will be pk

for doing this add this code in your routes.php file

Router::connect(
'/:user_name', array(
    'controller' => 'users', // default controller 
    'action' => 'profile' // default action 
), array(
    'pass' => array('user_name'), username to your action
    'username' => '[a-zA-Z0-9@_\s]+' // regular expression match parameter 
    )
    );

where user_name will be the last parameter in url users is the controller and profile is the action

How to redirect from 404 page to home page automatically after a delay

If user try to open a non existing url then website show 404 page. It means page not found. if you want to rediret automatically on other page if he is on 404 page then you can use javascript function

setTimeout(function() { window.location='/'; }, 5000);

In this code setTimeout is a javascript function and 5000 is the time ( 5 seconds ). after 5 seconds the page will redirect on home page. you can increase or decrease the time and change the url for redirection

Create error 404 page if page not found or url not exist in laravel

In laravel if you put wrong url then it will give defualt error. This error message always same in all types of error. But if you want to redirect on custom 404 page in case of if user put wrong url or url does not exist then you can create custom 404 page. by these steps

Go to /app/start/global.php and put the below code

App::missing(function($exception)
{
   return Redirect::action('ErrorController@error');
});

if you dont want use controller then

App::missing(function($exception)
{
   return Response::view('error.error', array(), 404);
});

Now open /app/routes.php and add

Route::get('/error404', 'ErrorController@error');

After save these files go in app/controllers and create a new controller and add

class ErrorController extends BaseController {

public $layout = 'layouts.admin';


public function error()
{
    $this->layout->title = "Laravel";
    $this->layout->content = View::make('error.error');
}



}

Now create View for this controller and action

go in /app/views and craate a folder name as error and create a file name as error.blade.php under error folder

Now in last open file app/config/app.php and search

debug' => false, in this file and replace it with <code>'debug' => true,

Now you can test this by puting wrong url

How to change defualt Auth fields in cakephp

The defualt auth fields in cakephp are username and password but if you want to use email or phonenumber at the place of username then you can modify your auth component in your controller. You can change model name also. cakephp Auth component defualt model is User. But by this method you can change this You nede to use this code in your controller

public $components = array('Session', 'Cookie',
    'Auth' => array(
        'authenticate' => array('Form' => array( 'userModel' => 'User',
                    'fields' => array(
                                        'username' => 'cellnumber',
                                        'password' => 'password'
                                        ),
                                                                                                //'scope' => array('is_active' => '1')
                                )
            ),
    ),
);

here i am using cellnumber as username

How to fetch record which inserted within one week in laravel

In my application i need to show last 7 days data in graph form and i am using laravel. For fetch 7 days old data first i need to know the date before 7 days. for get date before 7 days i used

$7days_ago_date = date('Y-m-d', strtotime('-8 days', strtotime(date('Y-m-d'))));

and now i have date before 7 days

i put this date in find query of laravel

$Tracker = Tracker::orderby('created_at', 'desc')->whereBetween( DB::raw('date(created_at)'), [$7days_ago_date, date('Y-m-d')] )->get();

and now i have all rows which inserted in last week to till

How to use pagination in laravel

If we use pagination in core php then we need to write more code but if you are using laravel then it is very eassy to implement pagination functionality and no need to write more code so let go to implement pagination in laravel

Here i have a controller name BlogcontentController

class BlogcontentController extends BaseController {

}

I want to fetch all blogs with pagination so i cretaed a function manageblogs

class BlogcontentController extends BaseController {

    public function manageblogs()
    {
        $this->layout->title = "Manage Blogs";

        $contentList = Blogcontent::orderby('created_at', 'desc')->paginate(15);

        $this->layout->content = View::make('content.mycontent')->with('contentList', $contentList);
    }

}

in this function Blogcontent is the model name i have set 15 blogs per page

now in view part for show pagination links put the below code at the bottom of your page

<?php echo $contentList->links(); ?>

now enjoy with your pagination functionality

How to create yii 1.x web application on window

If you are using yii 2 then no need to do this process but if you are using ver 1 then you can create yii application skeleton by doing these steps.when you will complete all the steps you can see a skeleton for yii application in your directory so lets go

Step 1. First download yii framework if you do not have

Step 2. Extract it in your web directory

Step 3. open cmd window and go in your webserver root folder. for example if you installed wamp in your C drive

cd C:\wamp\www\

Step 4. Now run command

php yii1.1.14/framework/yiic.php webapp myfirstapp

when process complete you can test it with url

http://localhost/myfirstapp/index.php

How to change height and width dynamically of all images on a web page with js

I was working on a elearning website and in this website div showing scrool if i upload a big image. I was showing images in a div. So i found the solution for this issue and i changed the height and width dynamicly with js code

The id of my div is pageDescription and i put this code

function managePagesContent()
{

    var image = document.getElementById("pageDescription").getElementsByTagName("img");
    for (var i=0; i 400)
        {
            image[i].setAttribute('class', 'pageImage');
        }
    }
}

Here a added a new class 'pageImage' on images if image width is greater then 400

.pageImage{
    width:400px;
}

and now call function

managePagesContent();

and now i have solved the issue

Thanks