How to using echo HTML::script in laravel 5.0 in Controller - html

I need to use
echo HTML::script('js/ckeditor/ckeditor.js');
in my controller and function, but error not found HTML
I am using larvel 5.0.
tnx

HTML and FORM are removed from Laravel 5+ to use them you have to include them in composer.json. And add an Alias and Service Provider in config\app.php
You can find them here
And as from laravel 5 {{}} is same as {{e('sting')}} //htmlentities
To output html you need to use {!! HTML::() !!} without htmlentities
And if you need to use echo
Simply wrap it to <?php ?> tags <?php echo HTML::() ?>
And if you use it Controller
you need to use like \Html::() or before Controller class add
use HTML;
HTML or Html depends on you Alias array in config\app.php
composer.json
"illuminate/html": "^5.0",
Config/app.php Service Provider
'Illuminate\Html\HtmlServiceProvider',
Config/app.php aliases
'Form' => 'Illuminate\Html\FormFacade',
'HTML' => 'Illuminate\Html\HtmlFacade',
Controller
<?php namespace App\Http\Controllers;
use HTML;
class SomeController extends Controller{
public function foo(){
echo HTML::();
}
}

Related

Laravel-generated email not formatting HTML correctly

I am struggling with email formatting issue, with Laravel.
I get the email content (HTML) from the database, which doesn't really matter, but then quotes get added around, the format is wrong and my email looks like this:
Here is my code, thanks a lot for your help!
I tried with
'content' => htmlspecialchars($content)
and
'content' => htmlentities($content)
but none work, and for the blade file:
<div>
{{!!$content!!}}
</div>
gives me an error. I also tried
<div>
{{{$content}}}
</div>
(also an error of unexpected character) and
<div>
{{$content}}
</div>
(here was the original one)
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cookie;
class InsuranceEmail extends Mailable
{
use Queueable, SerializesModels;
protected $attacheddoc;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($attacheddoc)
{
$this->attacheddoc=$attacheddoc;
}
/**
* Build the message.rubr
*
* #return $this
*/
public function build()
{
$name = Auth::user()->nom . " " . Auth::user()->prenom;
$sqlContent="SELECT texte from blabla";
$content = DB::connection('blabla')->select( DB::connection('blabla')->raw($sqlContent))[0]->texte;
$content = str_replace('#memberName#', $name, $content);
$content = str_replace('"', '', $content); //I tried this, without any hope ;)
return $this->from('contact#blabla.net')
->markdown('emails.blabla')->with([
'title' => "Email onject",
'memberName' => $name,
'content' => $content,
])
->attach($this->attacheddoc, array(
'as' => 'attacheddoc.pdf',
'mime' => 'application/pdf'));
}
}
I tried a few things to try and fix my email displaying incorrectly. In the end clearing my view cache solved my problem, which I hadn't seen anyone else suggest. This most likely wasn't your issue here but I will include it in my answer to hopefully help anyone else with my issue.
Publish the Laravel email views
php artisan vendor:publish --tag=laravel-mail
Make sure there are no indents in your html views
resources/views/vendor/mail/html
Make sure to escape any html inserted via variable
{!! $content !!}
Clear cached views
php artisan view:clear
As per Laravel Documentation:
By default, Blade {{ }} statements are automatically sent through
PHP's htmlspecialchars function to prevent XSS attacks. If you do not
want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}.
Reference:
Laravel -> Blade Templates -> Displaying Unescaped Data
In your emails.blabla view us elike this it will escape HTML element
{{{ $content }}}
or try
{!! $content !!}

Object values not being passed

I am using Laravel 5.6 and having an issue passing data to my blade file.
BlogController:
namespace App\Http\Controllers;
use App\Mail\Practice;
use App\Mail\Mailable;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Post;
use Session;
class BlogController extends Controller
{
public function getSingle($slug){
// Fetch from the DB based on Slug --first stops after one, get pulls everything
$post = Post::where('slug', '=', $slug)->first();
print_r($slug);
// return the view and pass in the post object
return view('blog.single')->withPost($post);
}
}
single.blade.php:
#extends('main')
#section('title', "| $post->title")
#section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>{{ $post->title}}</h1>
<p>{{ $post->body }}</p>
</div>
#stop
I verified the name and spelling in the DB (MySQL.) If I dd($slug) or print_r($slug) the results are correct.
However, if I do the same but use $title or $body it returns the error
Trying to get property of non-object (View: /Users/jesseburger/myproject/resources/views/blog/single.blade.php)
I have been able to verify its pulling an empty array by using print_r($post) but can't figure out why.
print_r($post) yields:
Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) )
Current route:
Route::get('blog/{slug}', [
'as' => 'blog.single',
'uses' => 'BlogController#getSingle'
])->where('slug', '[\w\d\-\_]+');
Your return statement is incorrect, you need to change this line:
return view('blog.single')->withPost($post);
To this, it should resolve your issue.
return view('blog.single')->with('post', $post);
First you are debugging your slug not your post. Try to debug your post to see if it was found. You are getting that error because the post doesn't exist at all. Abort if it doesn't exist.
if(!$post){
abort(404);
}

CakePHP 3.x - Associations Error- unable Linking Tables Together

i'm trying to Associate 2 tables:Jobes and Types. the table structure of types is very simple and includes only id and name ( 1.full-time 2.partial 3.freelance)
Jobs table, in addition to all other fields contains also the foreign key of Types named:type_id so Jobs belongsTo Types.
i follow the Cake convention but still the same error:
Jobs is not associated with Types...
i'v created JobsTable class
use Cake\ORM\Table;
class JobsTable extends Table{
public $name= 'Jobs';
public function initialize(array $config){
$this->belongsTo('Types');
}
}
declare it in the controller:
<?php
namespace App\Controller;
use App\Controller\AppController;
class JobsController extends AppController
{
public $name='Jobs';
public function index(){
//Get job info
$jobs =$this->Jobs->find('all')->contain(['Types']);
$this->set('jobs',$jobs);
}
}
and then past it to view (index.ctp):
<?php foreach ($jobs as $job) : ?>
<p> <?php echo $job->title; ?> <?php echo $job->types->name; ?></p>
<?php endforeach; ?>
Where is my mistake?
You need to set foreignKey too.
JobsTable.php
$this->belongsTo('Types', [
'foreignKey' => 'type_id'
]);
TypesTable.php
$this->hasMany('Jobs', [
'foreignKey' => 'type_id'
]);
To generate proper relations from CLI go to 'project_directory/bin' in command promot and hit following bake command:
cake bake types model // for types table
and
cake bake jobs model // for jobs table
See details here : CakePhp3 Code Generation With Bake

Json call in joomla 2.5 component

I'm making a joomla 2.5 component and i trying to set in model or controller (what's the most appropriate ?) a json response of my DB request (for later get the json with angularJS).
Here's my model (with DB response):
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modelList' );
class MediastoreModelList extends JModelList
{
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, type, designation', 'marque', 'prix');
$query->from('produits');
return $query;
}
}
My empty controller:
<?PHP
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MediastoreController extends JController
{
}
My view
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class MediastoreViewList extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
parent::display($tpl);
}
}
and my template
<?php
defined('_JEXEC') or die('Restricted access');
JHTML::script('media/com_mediastore/js/angular.min.js');
JHTML::script('media/com_mediastore/js/app.js');
?>
<?php
echo $this->items;
?>
<div class="content">
<p>Nothing</p>
</div>
How can i do that ?
Thanks a lot,
Antoine
Bit late .....
Hi I am working on something similar.
In your views (sitepart) add an other file called view.raw.php.
You can browse to this file by appending 'format=raw' at the end of the url.
For example: if your component view page in the browser is 'http://test.com/test( IF SEF is on ) then after appending th url shoul be like this
http://test.com/test?format=raw and in case SEF is not on the use & instead of ?
You can use this URL in the angularjs for http.get service.
In view.raw.php file make sure you just echo the results rather than parent::display($tpl);.
This did work for me.
PS: I am using angualrjs too but want to use the component with many menu items and the url keeps changing so am stuck on that part.
Hope this solves your issue.
Regards,
Jai

Google Map V3 Cakephp helper and multiple markers

I am using the Cakephp Google Map V3 Helper. I can get the google map to show up but the markers do not. Here is my view code:
<?php
echo $this->GoogleMapV3->map();
foreach ($allcondos as $condo) {
$options = array(
'lat' => $condo['Unit']['lat'],
'lng' => $condo['Unit']['lon']
);
$this->GoogleMapV3->addMarker($options);
}
?>
I know that if I just tell the app to echo out my $condo['Unit']['lat'] or ['lon'] it will do so in the foreach loop (so it is pulling my data). What I don't know how to do is how to write the code for the $options array. I have also tried this:
foreach ($allcondos as $condo) {
$lat=$condo['Unit']['lat'];
$lon=$condo['Unit']['lon'];
$options = array(
'lat' => $lat,
'lng' => $lon
);
$this->GoogleMapV3->addMarker($options);
}
How do I write this correctly?
A couple easy steps to get this to work:
Download
Download from https://github.com/dereuromark/cakephp-google-map-v3-helper and place the GoogleMapV3Helper.php file in /app/view/helper/GoogleMapV3Helper.php.
Load Helper
Either modify your appcontroller so that the top of it reads like the following:
<?php
class AppController extends Contoller{
public $helpers = array('Html','Javascript','GoogleMapV3');
}
?>
Or load it in a single controller by adding it to the helpers array as such:
<?php
class DemoController extends AppContoller{
function map() {
$this->helpers[] = 'GoogleMapV3';
# rest of your code
}
}
?
Include Scripts
Include Jquery in your header. Include the following as well:
<?php
echo '<script type="text/javascript" src="'.$this->GoogleMapV3->apiUrl().'"></script>';
?>
Create Map Container
Put this in your view where you want your map to appear. Feel free to modify the properties of the div.
<?php echo $this->GoogleMapV3->map(array('div'=>array('id'=>'my_map', 'height'=>'400', 'width'=>'100%'))); ?>
Note: you can change the default position of the map by including more options than just 'div':
<?php echo $this->GoogleMapV3->map(array('map'=>array(
'defaultLat' => 40, # only last fallback, use Configure::write('Google.lat', ...); to define own one
'defaultLng' => -74, # only last fallback, use Configure::write('Google.lng', ...); to define own one
'defaultZoom' => 5,
),'div'=>array('id'=>'my_map', 'height'=>'400', 'width'=>'100%'))); ?>
Add markers
Can be in a loop or whatever, but this is done in the view after your container is created.
<?php
$options = array(
'lat'=>40.770272,
'lng'=>-73.974037,
'title' => 'Some title', # optional
'content' => '<b>HTML</b> Content for the Bubble/InfoWindow' # optional
);
$this->GoogleMapV3->addMarker($options);
?>
note: only set the 'icon' key in the array if you want to use a custom image. Otherwise, they will not show up.
Include the script for the markers
<?php echo $this->GoogleMapV3->script() ?>
All done!
Alternately, you can use finalize() instead of script() if you do not want to echo the javascript right away, but write it to the buffer for later output in your layout:
<?php $this->GoogleMapV3->finalize(); ?>
See http://www.dereuromark.de/2010/12/21/googlemapsv3-cakephp-helper/ for details.