dongshao2967 2016-01-19 00: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-19 20: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 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部