【问题标题】:Controller methods in LaravelLaravel 中的控制器方法
【发布时间】:2014-11-29 22:40:09
【问题描述】:

过去 2 年我一直在开发 codeigniter 应用程序,自从我从 codeigniter 本身开始我的 mvc 模式样式以来,现在我不是一个重度命令行用户,所以起初我确实有一些使用 codeigniter 的学习曲线,但我越过了它并且开始使用 code igniter 开发应用程序,因为我们不需要进行很多配置,所有内容都在一个 zip 文件中,但现在不幸的是 codeigniter 已经死了,我只是我团队中的一个人,我不得不依赖其他第三方工具受到其他人的信任,所以我决定改用laravel,现在开始时很难从codeigniter迁移,因为composer和其他所有东西,但不知何故我也越过了,但我现在对路由和其他东西感到困惑和我尝试了很多教程,但我仍然看不到如何从我管理学生的应用程序迁移,他们可以更改电子邮件,更改电话号码更新的东西,在 codeigniter 中这很容易,但我不如何在路由中处理这些东西laravel,现在这个问题对于已经在使用 laravel 的社区来说听起来很愚蠢,但如果你从我的角度来看,它会影响我的面包和黄油。这就是我在codeigniter中使用的方法

class Student extends CI_Controller{
    // usual piece of code of constructor
    function update_email()
    {
          // piece of code to update email
    }
}

但是现在有了 laravel 路由系统,我不知道如何处理这个资源控制器看起来像这样

<?php

class StudentController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {

    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }


}

现在每个部分都还可以,但是我该如何处理我只需要更新电子邮件地址或只更新电话号码等的事情

【问题讨论】:

    标签: php codeigniter laravel laravel-4 laravel-routing


    【解决方案1】:

    实际上,在您的问题中,您有一个 RESTful 控制器,此时您可能会感到困惑,因为您是 Laravel 的新手并使用过 CI,所以您可能非常习惯于自动化 @987654325 @ 没有路线的映射。在这种情况下,为方便起见,我建议您使用普通控制器,这与您在CI 中所做的几乎相同,但在Laravel 中,您必须为每个操作声明route。因此,只需创建如下两条路线:

    Rouite::get('something/edit/{id}', array('uses' => 'StudentController@edit', 'as' => 'edit.record'));
    
    Rouite::post('something/update/{id}', array('uses' => 'StudentController@update', 'as' => 'update.record'));
    

    然后创建一个类/控制器并声明这些方法:

    class StudentController extends baseController {
    
        // URL: http://example.com/something/edit/10 and it'll listen to GET request
        public function edit($id) {
            // $record = Load the record from database using the $id/10
            // retuen View::make('editform')->with('record', $record);
        }
    
        // URL: http://example.com/something/update/10  and it'll listen to POST request
        public function update($id) {
            // Update the record using the $id/10
        }
    }
    

    在您的表单中,您需要使用http://example.com/something/update/10 作为action,您可以使用route('update.record')url('something/update/10') 在表单中生成action。阅读更多Laravel Documentation

    【讨论】:

      【解决方案2】:

      您在这里创建(或者更确切地说是生成)的内容称为Restful controller。这是管理 CRUD 操作(创建/读取/更新/删除)的某种“标准方式”。但是,没有必要这样做。您可以在控制器中添加您想要的任何方法,而根本不使用 Restful 控制器。那是你的选择。

      你可以在你的函数中创建新方法

      function updateEmail() 
      {
         // do here whatever you want
      }
      

      并在您的 routes.php 文件中添加新路线:

      Route::match(array('GET', 'POST'), '/changegemail', 'StudentController@updateEmail');
      

      现在您可以为此方法编写代码,它会起作用。

      【讨论】:

        猜你喜欢
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 2018-01-12
        • 2015-08-02
        • 2020-05-15
        • 2017-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多