Laravel 5 - 控制器中的视图('test')无法正常工作

时间:2022-08-23 10:14:15

I am new in Laravel. I read documentation and googling but I can't find answer for my problem.

我是Laravel的新人。我阅读文档和谷歌搜索,但我找不到我的问题的答案。

Im trying to render a simple view test.blade.php (located at resources/views).

我试图渲染一个简单的视图test.blade.php(位于资源/视图)。

a) first try: in app/Http/routes.php I wrote

a)首先尝试:在app / Http / routes.php中我写道

Route::get('/', function(){
    return view('test');
});

It's working. Hurray! I see my simple site

它的工作原理。欢呼!我看到了我简单的网站

b) second try: in app/Http/routes.php i wrote

b)第二次尝试:在app / Http / routes.php中写道

Route::get('/', 'WelcomeController@index');

I have created a controller WelcomeController.php in app/Http/Controllers Inside controller I wrote:

我在app / Http / Controllers里面创建了一个控制器WelcomeController.php我写道:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class WelcomeController extends Controller
{
    public function index()
    {
        return view('test');
    }
}

And....this is not working, I see only blank page. Ok I was digging and digging and trying to understand what's the problem. I changed retun view('test') to return view()->make('test'); but this is not working too - it generates only blank page....

而且....这不起作用,我只看到空白页面。好的,我正在挖掘和挖掘并试图了解问题所在。我改变了retun view('test')来返回view() - > make('test');但这也不行 - 它只生成空白页....

Then I simply changed retun to echo: echo view('test'); It is working like charm.

然后我简单地将retun改为echo:echo view('test');它像魅力一样工作。

What am I doing wrong? all examples in the internet shows return view('..') not echo view('...') (which is ugly)...

我究竟做错了什么?互联网上的所有例子都显示了返回视图('..')而不是echo视图('...')(这很难看)......

Please help me :)

请帮帮我 :)

2 个解决方案

#1


1  

After few hours of digging I got it!

经过几个小时的挖掘,我得到了它!

I decided to create second project and using composer to install laravel in another directory. Next I opened great tool names Meld which I used to compare these two installations. I noticed that there are some diferences in main controller Controller.php located at app/Http/Controllers

我决定创建第二个项目并使用composer在另一个目录中安装laravel。接下来,我打开了很棒的工具名称Meld,我用它来比较这两个装置。我注意到位于app / Http / Controllers的主控制器Controller.php存在一些差异

In my first installation (that with problems) it was:

在我的第一次安装(有问题)中,它是:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public $auto_render = true;

    /**
     * Execute an action on the controller.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function callAction($method, $parameters)
    {
        parent::callAction($method, $parameters);
    }    
}

And in the second installation:

在第二次安装中:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

I decided to remove callAction from Controller.php because it only call parent method. Then I checked my page in browser...and

我决定从Controller.php中删除callAction,因为它只调用父方法。然后我在浏览器中检查了我的页面...和

Hurrrayy it's working....return view('test') working as it should...

Hurrrayy它正在工作....返回视图('test')正常工作......

I think, that in some previous version of Laravel this method was added, and in the next someone removed it...but why it blocks rendering template?

我想,在Laravel的某些早期版本中添加了这个方法,并且在下一个人中删除了它...但是为什么它会阻止渲染模板?

It happens because overwritten function did not return result... When I changed that function like this:

它发生是因为覆盖函数没有返回结果...当我改变这样的函数时:

public function callAction($method, $parameters)
{
    return parent::callAction($method, $parameters);
}

That was the solution! Everything is now working great.

那是解决方案!现在一切都很好。

I want to thank you all, a specially maytham-mahiam and revo for your big help.

我要感谢你们所有人,特别是maytham-mahiam和revo,感谢你们的大力帮助。

#2


0  

Another approach is routes definition using array

另一种方法是使用数组的路由定义

Try this:

尝试这个:

You could also specify the controller and action within array.

您还可以在数组中指定控制器和操作。

Here we are saying the route to use the index function within the WelcomeController

这里我们说的是在WelcomeController中使用索引函数的路由

Add in routes.php

添加routes.php

Route::get('/', [ 'uses' => 'WelcomeController@index' ]);

Hope this is helpful.

希望这有用。

#1


1  

After few hours of digging I got it!

经过几个小时的挖掘,我得到了它!

I decided to create second project and using composer to install laravel in another directory. Next I opened great tool names Meld which I used to compare these two installations. I noticed that there are some diferences in main controller Controller.php located at app/Http/Controllers

我决定创建第二个项目并使用composer在另一个目录中安装laravel。接下来,我打开了很棒的工具名称Meld,我用它来比较这两个装置。我注意到位于app / Http / Controllers的主控制器Controller.php存在一些差异

In my first installation (that with problems) it was:

在我的第一次安装(有问题)中,它是:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public $auto_render = true;

    /**
     * Execute an action on the controller.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function callAction($method, $parameters)
    {
        parent::callAction($method, $parameters);
    }    
}

And in the second installation:

在第二次安装中:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

I decided to remove callAction from Controller.php because it only call parent method. Then I checked my page in browser...and

我决定从Controller.php中删除callAction,因为它只调用父方法。然后我在浏览器中检查了我的页面...和

Hurrrayy it's working....return view('test') working as it should...

Hurrrayy它正在工作....返回视图('test')正常工作......

I think, that in some previous version of Laravel this method was added, and in the next someone removed it...but why it blocks rendering template?

我想,在Laravel的某些早期版本中添加了这个方法,并且在下一个人中删除了它...但是为什么它会阻止渲染模板?

It happens because overwritten function did not return result... When I changed that function like this:

它发生是因为覆盖函数没有返回结果...当我改变这样的函数时:

public function callAction($method, $parameters)
{
    return parent::callAction($method, $parameters);
}

That was the solution! Everything is now working great.

那是解决方案!现在一切都很好。

I want to thank you all, a specially maytham-mahiam and revo for your big help.

我要感谢你们所有人,特别是maytham-mahiam和revo,感谢你们的大力帮助。

#2


0  

Another approach is routes definition using array

另一种方法是使用数组的路由定义

Try this:

尝试这个:

You could also specify the controller and action within array.

您还可以在数组中指定控制器和操作。

Here we are saying the route to use the index function within the WelcomeController

这里我们说的是在WelcomeController中使用索引函数的路由

Add in routes.php

添加routes.php

Route::get('/', [ 'uses' => 'WelcomeController@index' ]);

Hope this is helpful.

希望这有用。