简体   繁体   English

在laravel 4中路由

[英]Routing in laravel 4

I have a section called as reports and under this there are around 40-50 reports. 我有一个称为报告的部分,在此之下有大约40-50个报告。

For each report i need to define a separate class/controller. 对于每个报告,我需要定义一个单独的类/控制器。

I have created a reports folder under controllers and added a ReportsController class under it. 我在控制器下创建了一个reports文件夹,并在其下添加了一个ReportsController类。 My routes route to this ReportsController's add method and based upon the parameter passed it call's the different report class (like ContainerReportController) add method. 我的路由路由到此ReportsController的add方法,并根据传递的参数调用不同的报告类(例如ContainerReportController)的add方法。

Example code: 示例代码:

controllers/reports/ReportController.php

    class ReportController extends BaseController {
     public function add($type)
     {
        $controller = Str::studly($type) . 'Controller';
        return $controller::add();
     }
    }


controllers/reports/ContainerReportController.php

    class ContainerController extends BaseController {
     public function add()
     {
         return "Report will be added here and redirected to main page".
     }
    }

I did it this way because it will not be possible for me to define routes to each of this different types of reports (like ContainerReportController), i defined a single route to the main RportController and from there i call different controllers. 我这样做是因为我无法定义到每种不同类型的报告的路由(例如ContainerReportController),我定义了到主RportController的一条路由,并从那里调用不同的控制器。

I want to know if i am doing it in right way or if i can have something else which can help me better my code. 我想知道我是否以正确的方式进行操作,或者是否可以使用其他可以帮助改善我的代码的方法。

FYI - I run composer dump-autoload after adding each Reports in controller/report directory so that the report class is autoloaded while the application runs. 仅供参考-在控制器/报告目录中添加每个报告后,我运行composer dump-autoload ,以便在应用程序运行时自动加载报告类。

Please help improve this coding. 请帮助改善此编码。

Thanks, Nikhil 谢谢,Nikhil

I think your code is not bad, but here's some improvements: 我认为您的代码还不错,但是有一些改进:

So you have a route: 所以你有一条路线:

Route::get('report/{type}', 'ReportsController@report');

A report controller which will instantiate your report and not use it statically, this is testable: 一个可以实例化您的报告而不是静态使用它的报告控制器,这是可测试的:

class ReportsController extends BaseController {

    public function report($type)
    {
        $report = Str::studly($type) . 'Report';

        return with(new $report)->add();
    }

}

Many report classes extending a BaseReport class. 许多报表类扩展了BaseReport类。

This is not a controller because controllers should not talk to each other. 这不是控制器,因为控制器不应相互通信。 A controller basically receives a request pass it to a service class or a model, get the procesed data and send it to a view. 控制器基本上接收到将请求传递给服务类或模型的请求,获取处理的数据并将其发送到视图。

class ContainerReport extends BaseReport {

    public function add()
    {
        $this->reportData = "Report will be added here and redirected to main page".

        return $this->render();
    }

}

And a BaseReport class, which will be extended by all report classes, to do the common things your reports will surely do: 还有一个BaseReport类,该类将由所有报告类扩展,以执行您的报告肯定会做的普通事情:

abstract class BaseReport {

    protected $reportData;

    public function render()
    {
        return $this->reportData;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM