Laravel 5.4 master blade: show data from database in footer - php

I have created a master.blade.php file as follow:
<!DOCTYPE html>
<html lang="en">
<head>
#include('layouts.head_files')
</head>
<body>
<!-- START MAIN WRAPPER -->
<div id="wrapper">
<!-- SEARCH FORM START -->
#yield('info_panel')
<!-- SEARCH FORM END -->
<!-- Start Page Content -->
<div class="recent-job">
<div class="container">
<div class="row">
<div class="col-md-12">
#yield('main_content')
<div class="spacer-2"></div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- End Page Content -->
</div>
<!-- END MAIN WRAPPER -->
#include('layouts.footer_files')
</body>
</html>
This is my footer_file.blade.php file
<script type="text/javascript">
$(document).ready(function(){
$('input.location').typeahead({
name: 'location',
local: ['London','Birmingham','Manchester', 'Liverpool']
});
});
</script>
<script src="<?=asset('assets');?>/bootstrap/js/bootstrap.min.js"></script>
<script src="<?=asset('assets');?>/js/jquery.easytabs.min.js" type="text/javascript"></script>
<script src="<?=asset('assets');?>/js/modernizr.custom.49511.js"></script>
<script type="text/javascript" src="<?=asset('assets');?>/jquery.min.js"></script>
<script type="text/javascript" src="<?=asset('assets');?>/js/typeheads.min.js"></script>
As you can see I have an array of some cities in a function in my footer_file files. I have hard coded it for now but I want to fetch the list of all cities from database and show it here. This footer is included in every page. I am finding it difficult to do it in Laravel. It was easy in Codeigniter. Can I access the database directly from my view? Please help.

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them.
public function boot()
{
$data = App\Model::get();
View::share('data', $data);
}
https://laravel.com/docs/5.4/views#sharing-data-with-all-views
Alternative to this solution is using view composer:
View::composer('layouts.footer_files', function ($view) {
....
});

Related

Remove script tag from url YII 1.17

I have a yii 1.17 application. Problem is any one can inject scripts in url. Ex: If user puts this after page url ?redir=%3C/SCript%3E%3CsvG/onLoad=prompt(7)%3E application gives popup with given message.
How can i avoid this?
Main Layout:
<html lang="en">
<head>
</head>
<body>
<?php ?>
<app-header></app-header>
<md-content id="wrapper" layout-fill layout-margin layout-padding>
<section class="grid_outer">
<?php echo CHtml::encode($content); ?>
<div class="md-grid-container md-grid-padding">
</div>
</section>
<app-footer></app-footer>
</md-content>
</body>
</html>
View File:
<app-login></app-login>

PHP Laravel Templating example

My objection is to create a full example out of the Laravel Toolbox Kit.
I want to establish a pageset of a Controller passing data to a blade site when a correctly routed address is called.
Here is my code:
routes.php
Route::get('/game/start', function () {
return view('start');
});
GameController.php
class GameController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function Start()
{
$file = fopen("levels.dat", "r");
if($file == false)
return view('start', ['levels' => "Couldn't open file"];
$filesize = filesize($file);
$filetext = fread($file, $filesize);
$fclose($file);
$levels = str_getcsv($filetext,",");
return view('start', ['levels' => $levels,
'levelsLength' => count($levels)]);
}
}
A game.blade.php. Here also the JS references are bleeding.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Conway's Game Of Life - Game</title>
<!-- CSS And JavaScript -->
<script type="text/javascript" src="/../../vendor/twitter/bootstrap/dist/js/bootstrap.min.js">
</script>
<script type="text/javascript" src="/../../vendor/components/jquery/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/../../vendor/twitter/bootstrap/dist/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<nav class="navbar navbar-default">
<!-- Navbar Contents -->
</nav>
</div>
#yield('content')
</body>
</html>
Then start.blade.php
#extends('layouts.game')
#section('content')
<h2>#yield('Title')</h2>
<h3>#yield('Message')</h3>
<div id="first-col">
Please select the layout you want to play with.
<form id="layout-selector" method="POST">
<!-- Watch if this dropdownSelectList works -->
<label for="selectorDropDown"> Please select the layout you want to play with. </label>
<select name="dropDownList">
<!-- This {{$level}} is a string of the Name of the Level -->
#for($i = 0; $i < $levelsLength; $i++)
<option value="{{$levels[$i]}}">{{$levels[$i]}}</option>
#endfor
</select>
<input type="submit" action="public/game/level"/>
</form>
</div>
<div id="second-col">
<img id="lightUp" style="display:none" src="../img/lightUp30.png"/>
<img id="putOut" style="display:none" src="../img/putOut30.png" />
<canvas id="createCanvas" style="">
Sorry, your browser doesn't support Canvas! Try it in another type!
</canvas>
<script type="text/javascript" src="../js/startGameScript.js"></script>
</div>
#endsection
So I would like to have a working site,since now it doesn't render. Thanks for your appreciated time and help. Any further explanation for request!
You just can't #yield inside a #section
Replace this lines
<h2>#yield('Title')</h2>
<h3>#yield('Message')</h3>
with this
<h2>{{ $Title }}</h2>
<h3>{{ $Message }}</h3>
Assuming you have $Title & $Message in your blade template.
Now you can also extend a template
<!-- Stored in resources/views/layouts/master.blade.php -->
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
<!-- Stored in resources/views/child.blade.php -->
#extends('layouts.master')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
In this example, the sidebar section is utilizing the #parent directive to append (rather than overwriting) content to the layout's sidebar. The #parent directive will be replaced by the content of the layout when the view is rendered.
More details can be found here https://laravel.com/docs/5.1/blade#template-inheritance
You will first have to fix this block:
<script type="text/javascript" src="/../../vendor/twitter/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/../../vendor/components/jquery/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/../../vendor/twitter/bootstrap/dist/css/bootstrap.min.css" />
Files inside laravel's vendor folder cannot be referenced from blade.
Link to hosted libraries instead:
Bootstrap
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
For latest versions:
https://www.bootstrapcdn.com/
https://developers.google.com/speed/libraries/

PHP JSON object will render on local host but not from remote server

I am creating a really simple mobile app for our local track club. The app seems to be working fine when I test it on my localhost but when I change the links to point to my remote server and then package the app nothing seems to render. I am hoping that this is a really easy fix. Here is a page that I would like to populate using a JSON object into a HTML frame using Javascript.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>United Track Club</title>
<link href="jquery-mobile/jquery.mobile-1.0.1.min.css" rel="stylesheet" type="text/css"/>
<!-- This reference to phonegap.js will allow for code hints as long as the current site has been configured as a mobile application. -->
<script src="jquery-mobile/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/phonegap.js" type="text/javascript"></script>
<script src="jquery-mobile/jquery.mobile-1.0.1.min.js" type="text/javascript"></script>
<!--a name="viewport"content="width=device-width, initial-scale=1"> -->
</head>
<body>
<div data-role="page" id="runnerListPage">
<div data-role="header">
<h1>Runners2</h1></div>
<div data-role="content">
<ul id="runnerList" data-role="listview" data-filter="true">
</ul>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Home</li>
<li>Search</li>
<li>Schedule</li>
</ul>
</div>
</div>
</div>
</body>
</html>
Here is the JS that I created to populate the above code.
$.getJSON('http://unitedtrack.org/Mobile/TF/getrunnerlist.php', function(data){
var object = data.items,
runnerCount = object.length,
target = document.getElementById('runnerList'),
i;
if (runnerCount>0){
for (i =0 ; i< runnerCount; i=i+1){
var unitrun=object[i],
EventDt = unitrun.First_Nm,
MeetNm = unitrun.Last_Nm;
target.innerHTML +='<li>'+ EventDt +', ' +MeetNm +' </li>';
}
}
else {
alert('there are no runners');
}
});
target.addEventListener("click",data,false);
Thank you in advance for your help.
You need to set proper headers on the page you are querying (e.g. http://unitedtrack.org/Mobile/TF/getrunnerlist.php).
You need to set Access-Control-Allow-Origin header. See the link for more informations about cross-origin requests.

Why would javascript not work when the page is fully rendered but works when I get fatal PHP errors?

I've been attempting to use jquery and jqueryui to make tabs on my website. However, I can't seem to get them to work. The main page is in PHP, and I am using the Codeigniter framework. If the page fully renders, then the tabs won't work. If I change something that creates a fatal error in the php the tabs appear. While I was attempting to figure out what was going on I created a very basic page with only the jquery demo script, and it wouldn't work either. If it makes any difference, I am hosting on HostGator.
Please advise.
Header:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sign Up!</title>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php echo base_url();?>css/jquery-ui-1.8.11.custom.css" type="text/css" media="screen" />
<!-- Java includes -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>
<div class = "header">
<span class="nav_bar"><?php if($this->session->userdata('is_logged_in')){ echo 'Welcome, ' . ucfirst($this->session->userdata('first_name')). " " . ucfirst($this->session->userdata('last_name')) . ' | ' . anchor('site/logout' , 'Logout');} else { echo anchor('site/is_logged_in', 'Login');} ?></span>
</div>
<div class="content">
Body:
<!-- Tab Script -->
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
<!-- end Tab Script -->
<div id="tabs">
<ul>
<li>All Contacts</li>
<li>Place Holder Tab</li>
</ul>
<div id="tabs-1">
<?php $this->load->view('all_contacts_tab_view'); ?>
</div>
<div id="tabs-2">
Place Holder tab
</div>
</div>
Footer:
</div> <!-- end content div -->
<div class="footer">
<div class="footer_left">
<div id="copyright"> © 2011 NetworkIgniter. All rights reserved. NetworkIgniter, networkigniter.com and the all designs are trademarks of NetworkIgniter. Created with CodeIgniter and hosted on HostGator.</div>
<div id="legal">Terms and Conditions | Privacy Policy</div>
<div id="benchmarking">{elapsed_time} | {memory_usage}</div>
</div>
</div>
</body>
</html>
I did track down a java error after all, but I'm not sure how to fix it.
Error: $("#tabs").tabs is not a function
Line: 23
it looks like your calling tabs before it generated I would put that in doc ready
$(document).ready(function() {
$( "#tabs" ).tabs();
});
Figured it out.
It was the Google tracking script at the end. It was claiming the $. the reason why it was working when php crashed was because it wasn't getting down to the footer where the Google script was.
Errors on tabs are often due to a missing end of tag such as a </div> or </li> or whatever. Check the code generated by the PHP script to see if everything's fine. You might wanna use the Developper Tools Plugin on FireFox to detect any validation problems (including the missing tags).

How could I improve this template system?

At the moment, I have a base HTML template file. When ever I want to make a new page, I copy the template and place some require_once statements in between specific tags. I was wondering if there's a better way that would make it unnecessary to copy the template each time. Here's a typical example:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/second.css" />
<script language="JavaScript" type="text/javascript"
src="js/validation_functions.js"></script>
<title>Order a Ticket for the ball</title>
</head>
<body>
<div id="banner">St. Tom's Ambulance Ball</div>
<!-- START[container] -->
<!-- "body" -->
<div id="container">
<!-- START[header] -->
<div id="header">
<!-- header -->
<div id="header_text">introduction</div>
<div id="header_cell2">the process</div>
<div id="header_cell3">start</div>
</div>
<!-- END[header -->
<!-- START[content] -->
<!-- "other container" -->
<div id="content">
<!-- START[form] -->
<div id="form">
<?php
require_once(realpath($config["directories"]["views"]."/index.form.view.php"));
?>
</div>
<!-- END[form] -->
<!-- START[data] -->
<!-- "main content" -->
<div id="data">
<?php
require_once(realpath($config["directories"]["views"]."/index.data.view.php"));
?>
</div>
<!-- END[data] -->
<!-- START[side] -->
<div id="side">
<?php
require_once(realpath($config["directories"]["views"]."/index.side.view.php"));
?>
</div>
<!-- END[side] -->
</div>
<!-- END[content] -->
<!-- START[footer] -->
<div id="footer">
<!-- footer -->
<div id="footer_text">
<ul>
<li>home</li>
<li>partners</li>
<li>projects</li>
<li>contact us</li>
</ul>
</div>
<div id="footer_cell2"> </div>
<div id="footer_cell3"> </div>
</div>
<!-- END[footer] -->
</div>
<!-- END[container] -->
</body>
</html>
EDIT: I have taken note of your suggestions to use GET. The new idea is to have each request url formed as index.php?page=page_name. This request would then be dealt with by a main controller which then sets the variables of the template based on the value of $_GET['page']. For this, the template will now be:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/second.css" />
<script language="JavaScript" type="text/javascript"
src="js/validation_functions.js"></script>
<title><?php h($title) ?></title>
</head>
<body>
<div id="banner">St. Tom's Ambulance Ball</div>
<!-- START[container] -->
<!-- "body" -->
<div id="container">
<!-- START[header] -->
<div id="header">
<!-- header -->
<div id="header_text"><?php h($header_1) ?></div>
<div id="header_cell2"><?php h($header_2) ?></div>
<div id="header_cell3"><?php h($header_3) ?></div>
</div>
<!-- END[header -->
<!-- START[content] -->
<!-- "other container" -->
<div id="content">
<!-- START[form] -->
<div id="form">
<?php
require_once(realpath($view_1));
?>
</div>
<!-- END[form] -->
<!-- START[data] -->
<!-- "main content" -->
<div id="data">
<?php
require_once(realpath($view_2));
?>
</div>
<!-- END[data] -->
<!-- START[side] -->
<div id="side">
<?php
require_once(realpath($view_3));
?>
</div>
<!-- END[side] -->
</div>
<!-- END[content] -->
<!-- START[footer] -->
<div id="footer">
<!-- footer -->
<div id="footer_text">
<ul>
<li>home</li>
<li>partners</li>
<li>projects</li>
<li>contact us</li>
</ul>
</div>
<div id="footer_cell2"> </div>
<div id="footer_cell3"> </div>
</div>
<!-- END[footer] -->
</div>
<!-- END[container] -->
</body>
</html>
Note: h() is a function that first of all removes all undesired entity tags before echoing a string.
On a related note, at the top of each page I have some controller files which are included with require_once. I was wondering if it would be possible to implement a function that simply includes files based on a specific input string (name of the functionality/page) i.e "index" in this way:
function include_controller($page){
switch($page){
case "index":
require_once(realpath($config["directories"]["controllers"]."/index_.php"));
break;
case "checkout":
require_once(realpath($config["directories"]["controllers"]."/checkout_.php"));
break;
default:
break;
}
}
Instead of hard coding the includes into each file, you could have a controller file in which you pass the page to be displayed through a $_GET variable. The controller then handles the logic and includes the appropriate page or pages. This is the way a lot of MVC frameworks do it.
Edit: To answer your second question, instead of using a switch, you could just check to make sure the file exists. If it does, include that file, otherwise output an error ("Page doesn't exists" or something similar).
function include_controller($page){
if (file_exists($config["directories"]["controllers"]."/$page_.php")) {
// page exists, include it
require_once($config["directories"]["controllers"]."/$page_.php"));
} else {
// page not found
}
}
Obviously you should probably make the function a little more robust and probably limit the files that will be included to a certain directory or something. Also make sure you properly filter the $page variable so users aren't able to access any file.
Keep this one file as your template file. Then for all the functionality in your site always hit this file. Lets sat this file is index.php. So all functionality requests go to index.php. But with different parameters so for functionality A.
index.php?function=a
For functionality b
index.php?function=b
you can add more parameters also.
Now on the basis of a,b and the set of parameters see what files you want to include as require once.
Like the others already said, it would be better to use some kind of MVC framework. Or at least use a template engine (e.g. Smarty). Your example is ok though, for the 90ies :)
You can get by with one template if you choose a different way of specifying what page is being requested, such as using a GET variable. You can load the pages in a database and specify each of the included pieces, then have one php 'template engine' that loads the requested page from the database and outputs the template with the right includes.
If your server supports it, you can references to things you want to include on all pages in .htaccess:
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"
(Found this on codingforums.com)

Categories