ds15812330851 2017-07-21 05:00
浏览 117

如何使用laravel 5.4从web.php访问控制器内部的功能

I want to call static functions inside controller class from web.php.

In Controller,

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RoutePackageController extends Controller
{
    static function callingroutes() {
        Route::group(array('prefix' => 'account'), function() {
            Route::get('/userhome', function()
                {
                    return view('welcome');
                });
        });

     }
}

In web.php,

\App\Http\Controllers\RoutePackageController::callingroutes();

But it throws exception errors.

[Symfony\Component\Debug\Exception\FatalThrowableError] Class 'RoutePackageController' not found

  • 写回答

1条回答 默认 最新

  • dpikoto468637 2017-07-21 08:22
    关注

    You should define your routes inside web.php like so:

    Route::group(array('prefix' => 'account'), function() {
        Route::get('/userhome', 'WelcomeController@index');
    });
    

    No need to define the route inside a controller and then have the controller action inside an anonymous function.

    Now we define the controller, note how in web.php instead of an anonymous function we passed a string. This string lines up with the controller class name and method.

    class WelcomeController extends Controller
    {
        public function index()
        {
            return view('welcome');
        }
    }  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)