在视图 Laravel 中从控制器调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39813314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling function from controller in view laravel
提问by Mirza Chilman
this is a simple thing actually but since im new in laravel it trouble me, i have this function
这实际上是一件简单的事情,但由于我是 laravel 的新手,这给我带来了麻烦,我有这个功能
class HomeController extends Controller {
public $layout = 'layouts.master';
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('landing-page')
->with('title','Landing Page')
->with('users',User::member()->get());
}
<!-- HOW TO CALL getDate Function in my view -->
public function getDate(){
$mytime = Carbon::now()->format('f');
$response = array
(
'content' =>$mytime,
'status' =>'success',
);
return Response::json($response)->view('landing-page');
}
}
how to call it in my laravel view? i search all over the internet but i not quite understand since programming is new for me i already tried something like this in my view using routes {{url('date')}}
, {{$mytime}}
, but none working, well i can call a function if there's certain event happen like clicking button or else but if no certain event it's quite confusing me
如何在我的 Laravel 视图中调用它?我寻遍了互联网,但我不是很了解,因为节目是新的我,我使用的路线已经尝试过这样的事情在我看来{{url('date')}}
,{{$mytime}}
但没有工作,那么我可以调用一个函数,如果有某个事件发生像点击按钮或其他但如果没有特定的事件,这让我很困惑
<p>Month :{{url('date')}}</p>
<p>Month :{{$mytime()}}</P>
above are some ways i tried to call the function
以上是我尝试调用该函数的一些方法
UPDATE WHAT I'VE TRIED BASED on @tptcat answer and work
根据@tptcat 的回答和工作更新我的尝试
create helpers.php
located under files `app\helpers.php
创建helpers.php
位于文件`app\helpers.php
<?php
use Carbon\Carbon;
if (! function_exists('myGetDate')) {
function myGetDate()
{
$mytime = Carbon::now()->format('F');
return $mytime
}
}
composer.json
composer.json
"autoload": {
"files":[
"App/helpers.php"
],
calling function in my view
在我看来调用函数
{{myGetDate()}}
回答by tptcat
This isn't a hard and fast rule, but part of using a framework is to somewhat buy into its conventions and use them to your advantages.
这不是一个硬性规定,但使用框架的一部分是在某种程度上接受其约定并将它们用于您的优势。
Generally speaking, your Controllers are for working with HTTP (GET, POST, PUT, etc.). They're not designed to be indiscriminate ways to call methods from your Views.
一般来说,您的控制器用于处理 HTTP(GET、POST、PUT 等)。它们不是为了从视图中随意调用方法而设计的。
I would recommend doing something like this instead:
我建议改为这样做:
// app/Utilities.php
<?php
class Utilities
{
public static function getDate()
{
// your code
}
}
then in your view:
那么在你看来:
{{ Utilities::getDate() }}
or:
或者:
// app/helpers.php
<?php
if (! function_exists('myGetDate')) {
function myGetDate()
{
// your code
}
}
then in your view:
那么在你看来:
{{ myGetDate() }}
and then in composer.json
autoload whichever file you create:
然后在composer.json
自动加载您创建的任何文件中:
"autoload": {
"files": [
"app/Utilities.php"
]
}
or...
或者...
"autoload": {
"files": [
"app/helpers.php"
]
}
and then run composer dump-autoload
.
然后运行composer dump-autoload
。
Another way to approach this could be using Blade Service Injection(introduced in Laravel 5.1). This technically can be done with your controller:
另一种解决方法是使用刀片服务注入(在 Laravel 5.1 中引入)。这在技术上可以通过您的控制器完成:
// In your blade template
@inject('service', 'App\Http\Controllers\HomeController')
{{ $service->getDate() }}
But I'd still recommend not having a method in your controller in charge of returning this data if it's going to be called as a method from a Blade template. Using some type of service class would be more appropriate:
但是我仍然建议在你的控制器中不要有一个方法来负责返回这个数据,如果它要作为一个来自 Blade 模板的方法被调用。使用某种类型的服务类会更合适:
// app/Utilities.php
<?php
namespace App;
class Utilities
{
public function getDate()
{
// your code
}
}
// In your blade template
@inject('service', 'App\Utilities')
{{ $service->getDate() }}
and in this case you wouldn'tneed to add it to the files
autoload array in composer.json
.
在这种情况下,你不会需要将其添加到files
在自动加载数组composer.json
。
For what it's worth, not knowing anything else about your project, I would choose one of the first two options, and more likely the helpers.php
option.
对于它的价值,对您的项目一无所知,我会选择前两个选项之一,并且更有可能helpers.php
选择该选项。
回答by jeremykenedy
Try this:
尝试这个:
class HomeController extends Controller {
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$title = 'Landing Page';
$users = \User::member() - > get();
$mytime = \Carbon::now()->format('f');
return view('layouts.master.landing-page', [
'title' => $title,
'users' => $users,
'mytime' => $mytime
]
);
}
}
And to display it within the landing-page view you would access them with:
并在登陆页面视图中显示它,您可以通过以下方式访问它们:
{{ $title }}
{{ $users }}
{{ $mytime }}