So i have folder 'posts' where i have 3 blade templates. I want to use some variable in all this templates. Therefore i added array of my templates. How can i set a folder with this files instead of array ?
What i have:
view()->composer(['admin.posts.index' , 'admin.posts.edit' , 'admin.posts.create'], function($view){
$view->with('current_user', Auth::user());
});
What i want:
view()->composer('folder_name', function($view){
$view->with('current_user', Auth::user());
});
Perhaps you can try with a wildcard:
view()->composer('admin.posts.*', ...);
Related
I have file excel.php by the path /resources/lang/en/excel.php
Then in controller I tried to fetch word by key:
use Lang;
echo Lang::get('excel.idEvent');
Also I tried:
dd(echo __('excel.idEvent'));
Whats is right way to do that?
First, your excel.php file must be in the right format:
<?php
return [
'welcome' => 'Welcome to our application'
];
The right way to get it on your blade template in fact it is:
echo __('excel.welcome');
or
echo __('Welcome to our application');
The way to do it on your controller is:
use Lang;
Lang::get('excel.welcome');
If you are not using Facades: use \Illuminate\Support\Facades\Lang;
You can also use the trans() function, ex:
Route::get('/', function () {
echo trans('messages.welcome');
});
If you use JSON translation files, you might have to use __().
Here are all the ways to use:
#lang('...') // only in blade files
__('...')
Lang::get('...')
trans('...')
app('translator')->get('...')
Lang::trans('...')
They all defer to \Illuminate\Translation\Translator::get() eventually.
In laravel blade system when we want to include a partial blade file we have to write the full path every time for each file. and when we rename a folder then we will have to check every #include of files inside it. sometimes it would be really easy to include with relative paths. is there any way to do that?
for example we have a blade file in this path :
resources/views/desktop/modules/home/home.blade.php
and I need to include a blade file that is near that file :
#include('desktop.modules.home.slide')
with relative path it would be something like this :
#include('.slide')
is there any way to do this?
if someone still interest with relative path to current view file, put this code in the boot method of AppServiceProvider.php or any provider you wish
Blade::directive('relativeInclude', function ($args) {
$args = Blade::stripParentheses($args);
$viewBasePath = Blade::getPath();
foreach ($this->app['config']['view.paths'] as $path) {
if (substr($viewBasePath,0,strlen($path)) === $path) {
$viewBasePath = substr($viewBasePath,strlen($path));
break;
}
}
$viewBasePath = dirname(trim($viewBasePath,'\/'));
$args = substr_replace($args, $viewBasePath.'.', 1, 0);
return "<?php echo \$__env->make({$args}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
});
and then use
#relativeInclude('partials.content', $data)
to include the content.blade.php from the sibling directory called partials
good luck for everyone
you need to create custom blade directive for that, the native include directive doesn't work like that.
read this page to learn how to create custom blade directive :
https://scotch.io/tutorials/all-about-writing-custom-blade-directives
\Blade::directive('include2', function ($path_relative) {
$view_file_root = ''; // you need to find this path with help of php functions, try some of them.
$full_path = $view_file_root . path_relative;
return view::make($full_path)->render();
});
then in blade file you can use relative path to include view files :
#include2('.slide')
I tried to tell you the idea. try and test yourself.
There’s now a package doing both relative and absolute includes (lfukumori/laravel-blade-include-relative) working with #include, #includeIf, #includeWhen, #each and #includeFirst directives. I just pulled it in a project, it works well.
A sleek option, in case you want to organise view files in sub-folders:
public function ...(Request $request) {
$blade_path = "folder.subfolder.subsubfolder.";
$data = (object)array(
".." => "..",
".." => $..,
"blade_path" => $blade_path,
);
return view($data->blade_path . 'view_file_name', compact('data'));
}
Then in the view blade (or wherever else you want to include):
#include($blade_path . 'another_view_file_name')
Iam bulding a multi-page app using Laravel and Vue.Now, for example lets say I want to use some Laravel helper function like {{ trans('messages.welcome') }} inside a Vue component (which is in a separate .vue file) .. how to do that ?
Access the Laravel helper function from your Blade template, passing the result to your Vue component as a prop:
// app.blade.php
<message-component :message="{{ trans('messages.welcome') }}"></message-component>
// MessageComponent.vue
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
props: [ 'message' ],
}
</script
In fact there is a way, kind of.
Inside data I called Laravel helper method with php tags. Code:
<script>
let app = new Vue({
el: '#app',
data: {
app_url: "<?php echo app_url(); ?>"
});
</script>
Here is another way to do what I believe #rook99
is trying to accomplish. This is working great for me.
Set up your Helper Functions in a a new folder in the app folder. I created a new folder in the app folder called CustomStuff. Then I set up a file called FormHelper.php . Add your Helper function to this file. This is the path to the Helper function file: app\CustomStuff\FormHelper.php
// This is my Helper Function. Create an array of states
public static function States()
{
$states =['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA',
'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MH', 'MD', 'MA', 'MI',
'MN', 'MS','MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH',
'OK', 'OR','PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA',
'WV', 'WI', 'WY',];
return $states;
}
Set up a new controller. In the Controllers folder. Call it AppUtilityController Do not forget to add
use FormHelper;
at the top of the controller file. The following command in the command line will create the controller.
php artisan make:controller AppUtilityController
In the AppUtilityController add a function to be called from a root and return the Helper function data.
public function getHelperFunctions()
{
$data['states'] = FormHelper::States();
return $data;
}
set up a new route in your routes folder to call the getHelperFunctions
Route::get('/app-utilities/get-helper-functions',
'Admin\AppUtilityController#getHelperFunctions');
Next go to Vuejs component where you need the Helper Function.
Set up a function using created method so the function will get called when component loads.
Have the function make an api request to the url in the route. Which would be app-utilities/get-helper-functions. I am using axios to make the request.
The FormHelper functions will be returned as a javascript object. Simply use the object anywhere in the component.
In the future, whenever you want to add Helper function just add the new function to the FormHelper.php and AppUtilityController.php files and insert the method to grab the data in your Vuejs component.
I wrote a blog [post]: https://david.dukesnuz.com/blog/8/how-to-use-laravel-helper-functions-with-vuejs on this topic.
Just add script into blade template:
<script>
window.laravel_helper_result1 = '{{laravel_helper_function_1()}}';
window.laravel_helper_result2 = #json(laravel_helper_function_2());
</script>
Then you can use these js variables in your Vue.
In my Web.php I have :
Route::get('/regions' , 'RegionsController#show');
Route::get('/regions/{region}' ,'RegionsController#getDetail');
Route::get('/login' , function(){
return view('users');
});
Route::get('/regions/create',function(){
return view('/regions/create');
});
Route::get('create',function(){
return view('regions/create');
});
Route::post('/regions' , 'RegionsController#store');
Route::get('users' , function(){
return view('users');
});
But When I want to get the create View , the page is not found , and I know it is due to the GetDetail Method in RegionsController, So my Question is What to do to get the create view when I Type /regions/create ?
Try to switch the placese of routes. Put regions/create before regions/{parameter}
Make folder regions in views folder. Then make file create.blade.php in regions folder.
I am new in joomla, My code is like this
//on controller
function listing()
{
JRequest::setVar( 'view', 'hello' );
JRequest::setVar('hidemainmenu', 0);
parent::display();
}
//on view.html.php
i want to fetch this 'hidemainmenu'
How can i fetch can anyone help??
If the code above is that of your view.html.php file then you can pass the variable through to your template file by using a line like so:
$this->assignRef( 'hidemainmenu', $hidemainmenu);
Then in your tmpl/default.php file for example you can access this variable like so:
$this->hidemainmenu
If the code is in the Controller :
get the Variable in the view.html.php
as
$hidemainmenu = JRequest::getVar('hidemainmenu');
Try this in the view.html.php or default.php