我正在创建一个laravel 5.2包,以下是我的文件:
packages/
-Shreeji/
--Ring/
---composer.json
---src/
----Ring.php
----RingModel.php
----RingServiceProvider
----Views/
-----ringslist.blade.php
composer.json
{
"name": "shreeji/ring",
"description": "Simple",
"license": "MIT",
"authors": [
{
"name": "author",
"email": "email@gmail.com"
}
],
"autoload": {
"psr-4": {
"Shreeji\\Ring\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
"Illuminate/support": "~5"
}
}
Ring.php
namespace Shreeji\Ring;
use Illuminate\Http\Response;
Class Ring {
function __construct() {
}
public function get_all()
{
return view("ring::ringlist");
}
}
RingServiceProvider.php
namespace Shreeji\Ring;
use Illuminate\Support\ServiceProvider;
Class RingServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('ring', function($app){
return new Ring;
});
}
public function boot()
{
$this->loadViewsFrom(__DIR__ . '/Views', 'ring');
}
}
ringlist.blade.php
<!DOCTYPE html>
<html>
<body>
<h1>Welcome</h1>
</body>
</html>
在app/Http/Controllers
中我创建了一个这样的测试文件:
Ringcontroller.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Shreeji\Ring\Ring;
class RingController extends Controller
{
public function index()
{
$ring = New Ring();
$ring->get_all();
}
}
当我调用控制器时,浏览器会继续加载并系统崩溃。我不知道是否可以在任何控制器类之外使用view
。
如果我在 Ring.php 文件中调用view
时出错,请告诉我。
答案 0 :(得分:0)
你可以使用像作曲家Docs
这样的东西在RingServiceProvider中注册一个作曲家
use Illuminate\Contracts\View\Factory as ViewFactory;
public function boot(ViewFactory $view)
{
$view->composer('*', 'App\Http\ViewComposers\SomeComposer');
}
并在App\Http\ViewComposers\SomeComposer
use Illuminate\Contracts\View\View;
public function compose(View $view)
{
$view->with('count', '1');
}
玩它,基本上我使用它在特定视图上共享$变量,但也许这可以帮助你实现你想要的。
或者您可以use Illuminate\Contracts\View\View;
加载您需要的视图!
答案 1 :(得分:0)
我看到夫妻问题:
您想要使用视图,但您的软件包不会require
illuminate/view
个软件包。您需要将composer.json
文件更新为需要"illuminate/view": "~5"
。
view()
函数是Illuminate\Foundation\helpers.php
中包含的辅助方法。除非您只想为此函数依赖整个Laravel框架,否则您需要创建自己的view()
函数。代码在下面,你把它放在你的身上。
if (! function_exists('view')) {
/**
* Get the evaluated view contents for the given view.
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
function view($view = null, $data = [], $mergeData = [])
{
$factory = app(ViewFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($view, $data, $mergeData);
}
}
一旦你的视图工作正常,你可以整天制作视图,但如果你没有return
来自你的控制器的任何东西,你就不会看到任何东西。确保从控制器方法中返回一些内容。