Laravel 4:在包中使用视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14678209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:32:31  来源:igfitidea点击:

Laravel 4: Using views in a package

phplaravellaravel-4

提问by Al_

I've created a very basic app in Laravel 4, it's something I'll be reusing a lot in various projects so it made sense to convert it to a package before i got too far, but I'm struggling to make the changes to get it working, which I think is largely due to figuring out how to access the various objects that are normally available in an app, eg View::make

我在 Laravel 4 中创建了一个非常基本的应用程序,我将在各种项目中重复使用它,因此在我走得太远之前将其转换为包是有意义的,但我正在努力进行更改让它工作,我认为这主要是由于弄清楚如何访问应用程序中通常可用的各种对象,例如 View::make

I had the following code working in an app:

我有以下代码在应用程序中工作:

class PageController extends BaseController {

public function showPage($id)
{
            //do stuff
            return View::make('page/showPage')
                 ->with('id', $id)
                 ->with('page', $page);
}

for the package I have the following:

对于包裹,我有以下内容:

use Illuminate\Routing\Controllers\Controller;
use Illuminate\Support\Facades\View;

class PageController extends Controller {

public function showPage($id)
{
      //do stuff        
      return View::make('page/showPage')
                 ->with('id', $id)
                 ->with('page', $page);
}

However this does not load the blade template which is located at:

但是,这不会加载位于以下位置的刀片模板:

workbench/packagenamespace/package/src/views/page/showPage.blade.php

nor does this work:

这也不起作用:

return View::make('packagenamespace/package/src/page/showPage')

Also, I am wondering if what i have done with the use statements where I use the facade object correct, to me it seems like there should be a neater way to access things like the View object?

另外,我想知道我对使用外观对象的 use 语句做了什么正确的事情,对我来说似乎应该有一种更简洁的方法来访问 View 对象之类的东西?

回答by bstrahija

You should read the docs: http://four.laravel.com/docs/packages

您应该阅读文档:http: //four.laravel.com/docs/packages

Specifically the part explaining loading views from packages ;)

特别是解释从包加载视图的部分;)

return View::make('package::view.name');

If you don' want to use:

如果您不想使用:

use Illuminate\Support\Facades\View;

Just do:

做就是了:

use View;

Or even without the use statement:

或者甚至没有 use 语句:

\View::make('package::view.name');