Ad

How To Call View From An Other File Than Controller

- 1 answer

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 protected]"
     }
 ],
 "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.

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad