I have used this section of code in layout section of my Laravel project:
#php
$setting = setting();
#endphp
As it is a layout I didn't call this file anywhere but included it many times.
And wherever it is included it is showing me this error.
Error
Call to undefined function setting()
What went wrong here?
my setting model is:
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $table = 'setting';
protected $primaryKey = 'id';
}
?>
also my full layout file is Here
setting function is defined in the Helper.php file. here it is:
<?php
function setting(){
return \App\Model\Setting::first();
}
?>
how can I fix this? TIA
you don't need to use :
#php
$setting = setting();
#endphp
the composer has a files key (which is an array of file paths) that you can define inside of autoload. so you can add the path of the helpers file in the composer.json file:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Helpers.php"
]
},
then Run :
composer dump-autoload
after that in your layout you can use {{setting()->email}} or if you use $setting =setting(); in your layout use {{$setting->email}}
Assuming your Manager is properly configured and eloquent booted.
Your file does not get included via composers autoload. But you actually dont need to load it.
#php
$setting = \App\Model\Setting::first();
#endphp
Related
I have two files, one is my main js file called app.js and I have a file where I store all my js functions, called functions.js. As you can see on the image below.
But I want to include the functions.js file into the app.js file. So I googled on how to do it and this is what people said:
But my npm run dev says the file doesn't exist. But the path is correct. What am I doing wrong here, is there a other way to do it?
You can simply just create the file wherever you want to create it, and then export some properties or methods, and then import them in your app.js file, or in whatever file you need. Something like this :
//new_file.js
export const jokes = function() {
return ['funny', 'not really funny', 'boring']
}
export const heading = 'some global heading to be reused.'
And in your app.js file :
import { jokes, heading } from 'new_file.js'//specify actual path here .
console.log(jokes) // ['funny', 'not really funny', 'boring']
console.log(heading)//some global heading to be reused.
This tutorial might be helpful too .
http://www.2ality.com/2014/09/es6-modules-final.html
I'm trying to include "parameters.js" file into my angular 4 project.
You can see below my "parameter.js" file content :
window.myKey = "http://webserviceUrl/"
I want to include this file in my project and get "myKey" value in a typescript file before loading web service datas.
My "parameters.js" file as a specific structure and do not be changed (no json :'( ).
Is anyone as loading js into typescript with angular 4 ? How can i do that ?
Thanks for your help.
Include the script in index.html
<script src="/assets/parameters.js" />
And declare the variable in the component where you need to access it
declare let myKey: string;
According to David explainations, i have added my parameters script in my angular conf ".angular-cli.json" like this :
"scripts": [
"../directory/parameters.js"
],
I can read the value in typescript with the code :
declare let myKey: string;
Thank you David for you great response ! :)
You can Import js locally to ts file like this : import '../../../scripts/custom.js';
then declare method name in custom.js that you want to use like this 👍
declare var fAlert;
(declarations statements are usually put just after the import statements)
then in a place like ngOnInit directly use this method
fAlert();
I write project using Yii.I would like to receive advice on how to organize my JavaScript.I thought about several options:
Put all code in one file (app.js), create for each feature moduls and attach it to each page of project.But problem is that not all feature required in all pages and so in this case I have to load useless code and it is difficult to navigate among hundreds of lines.(I know that I can write in separate files and then "compile" all in one, but it does not solve problem of excess code)
Create for each feature separate file and manage it loading through Yii(attach only necessary files to page). But some feature require only couple lines of code and it seems irrational to create file for that.
You could use a mixing of Asset and layout
In a proper layout stored inside your_app\views\layouts\your_asset_filename.php
you could register a specific asset
use your_app\assets\YourAsset;
//use common\models\UserParam;
//use common\models\LandScope;
/* #var $this \yii\web\View */
/* #var $content string */
YourAssest::register($this);
?>
In your_app\asset you could configure the js files you needs
class YourAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
........
];
public $js = [
'your_js1.js',
'your_js2.js',
....
];
public $jsOptions = ['position' => \yii\web\View::POS_HEAD];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
and last your controller/action you can set the layout you need for each render
public function actionMyAction()
{
$this->layout = 'your_asset_filename';
...
return $this->render('your_view',[ ....]);
}
i want to add a reference to my scripts in a Laravel app. In my edit UI I have the reference like this:
{{ HTML::script('js/exam-definition.js') }}
{{ HTML::script('js/exam-definition-update.js')}}
The scrpits are in the folder: /public/js/exam-definition.js and the same for the other. But when I deploy in the browser the page give me this error:
http://test.dev/public/exam/js/exam-definition.js?=1406161079799
http://test.dev/public/exam/js/messages.js?=1406161079800
You can see how the URL changes (because asset take), append an "exam" folder that dont exists. Is something wrong? I have this same ref to the scripts in another blade template and there is no error when deploy.
This are my controller method:
/**
* Show the form for edit a Exam
*/
public function edit($id) {
$exam = ExamService::examDefinition($id);
$questions = $exam->getQuestions()->getResults();
return View::make('exam.edit')
->with('exam', $exam)
->with('questions', $questions);
}
And the route for this page:
Route::get('exam/edit/{id}',
array('as' => 'exam_edit',
'uses' => 'ExamDefinitionController#edit'
)
);
Thanks in advance!
You should use
{{ HTML::script('/js/exam-definition.js') }}
{{ HTML::script('/js/exam-definition-update.js')}}
This will create a reference starting from the web root, and not from your current directory where the file is at. This also explains why your code could work from another blade template.
I´ve added to my template.tpl.php file inside my drupal theme this line to elimiante the calling to a specific css file:
remove-stylesheets[all][] = modules/system/system.css
If I want to do that with a specific js file, could I just do this?:
remove-scripts[] = whatever.js
I´ve tried it, but it doesn´t seems to work...
Thanks for your help!
Try this code in your template file.
function phptemplate_preprocess_page(&$vars) {
$js = drupal_add_js();
unset($js['module']['sites/all/modules/yourmodule/yourjs.js']);
$vars['scripts'] = $js;
}
This function will be called before the template is processed and rendered to the browser. If you are not sure about the path to your js file, print the drupal_add_js() array and find the path to the required js.