dongshao2967 2016-01-19 08:33
浏览 135
已采纳

如何从控制器以外的其他文件中调用视图

I am creating a laravel 5.2 package, following are my files:

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>

And in app/Http/Controllers I have created a test file like this:

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();
 }
}

When I call the controller, browser keeps loading and crashed systematically. I don't know if I can use view outside any controller class like such.

Let me know if I did any mistake in calling view from Ring.php file.

  • 写回答

2条回答 默认 最新

  • duanbin198788 2016-01-20 04:01
    关注

    Couple issues I see:

    1. You want to use views, but your package does not require the illuminate/view package. You need to update your composer.json file to require "illuminate/view": "~5".

    2. The view() function is a helper method included at Illuminate\Foundation\helpers.php. Unless you want to depend on the entire Laravel framework just for this function, you will need to create your own view() function. The code is below, where you put it is up to you.

      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);
          }
      }
      
    3. Once you get the view stuff working, you can make views all day long, but if you don't return anything from your controller, you're not going to see anything. Make sure you return something from your controller methods.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题