首页  >  问答  >  正文

php实现restful api有什么框架使用方便?

php框架比较多,想实现restful api用哪个比较流行

高洛峰高洛峰2772 天前1704

全部回复(24)我来回复

  • 迷茫

    迷茫2017-04-10 16:28:38

    ZendFrame Work

    回复
    0
  • ringa_lee

    ringa_lee2017-04-10 16:28:38

    laravel ++++++++1

    回复
    0
  • 天蓬老师

    天蓬老师2017-04-10 16:28:38

    YII或者YII2.除了这两个,没有之一。为什么?因为我看见laravel还是什么的竟然用if-else来做Restfull
    关于实现RestFullAPI。看这里:
    https://github.com/evan108108/RESTFullYii
    这个是全部RESTFULL的,完全满足楼主的要求。看看它的这个扩展关于How it works说明:
    How it works
    RestfullYii adds a new set of RESTFul routes to your standard routes, but prepends '/api' .
    So if you apply RestfullYii to the 'WorkController' you will get the following new routes by default.
    [GET] http://yoursite.com/api/work (returns all works)
    [GET] http://yoursite.com/api/work/1 (returns work with PK=1)
    [POST] http://yoursite.com/api/work (create new work)
    [PUT] http://yoursite.com/api/work/1 (update work with PK=1)
    [DELETE] http://yoursite.com/api/work/1 (delete work with PK=1)
    为什么我会推荐YII。先看看这位的回答:
    http://segmentfault.com/q/1010000000500665#a-1020000000503507
    这位兄台肯定没有见过纯面向对象的形式,以为所有的人都像他一样动不动就if-else。还有看到laravel 这样竟然用if-else的实现我就笑了。
    YII实现Restfull没有用到所谓的if-else,所有的实现都是纯面向对象,读完他的代码,你就知道这设计的简直太他妈精妙了,然后你读别的框架的代码,你就会很高兴,因为他写的你完全看得懂,比如if-else。

    看了各个框架对RestfullAPI的实现,我才知道了哪些框架是真的牛逼,哪些框架是在装牛逼。
    不多说,看一看YII如何实现Restfull,我只贴上将一个PUT动作封装成一个类的代码代码,楼主再去看看所谓的laravel 的代码,高下立见:

    <?php
    Yii::import('RestfullYii.actions.ERestBaseAction');
    
    /**
     * Action For Rest Puts
     *
     * Provides the action for rest put behavior
     *
     * @category   PHP
     * @package    Starship
     * @subpackage Restfullyii/actions
     * @copyright  Copyright (c) 2013 Evan Frohlich (https://github.com/evan108108)
     * @license    https://github.com/evan108108   OSS
     * @version    Release: 1.2.0
     */
    class EActionRestPUT extends ERestBaseAction
    {
        /**
         * run
         *
         * Called by Yii for PUT verb
         * 
         * @param (Mixed/Int) (id) unique identifier of the resource
         * @param (Mixed) (param1) first param sent in the request; Often subresource name
         * @param (Mixed) (param2) Second param sent in the request: Often subresource ID
         */
        public function run($id=null, $param1=null, $param2=null) 
        {
            $this->finalRender(function($visibleProperties, $hiddenProperties) use($id, $param1, $param2) {
                    switch ($this->getRequestActionType($id, $param1, $param2, 'put')) {
                        case 'RESOURCES':
                            throw new CHttpException('405', 'Method Not Allowed');
                            break;
                        case 'CUSTOM':
                            return $this->controller->emitRest("req.put.$id.render", [$this->controller->emitRest(ERestEvent::REQ_DATA_READ), $param1, $param2]);
                            break;
                        case 'SUBRESOURCES':
                            throw new CHttpException('405', 'Method Not Allowed');
                            break;
                        case 'SUBRESOURCE':
                            return $this->controller->emitRest(ERestEvent::REQ_PUT_SUBRESOURCE_RENDER, [
                                $this->handlePutSubresource($id, $param1, $param2),
                                $param1,
                                $param2,
                                $visibleProperties,
                                $hiddenProperties,
                            ]);
                            break;
                        case 'RESOURCE':
                            return $this->controller->emitRest(ERestEvent::REQ_PUT_RESOURCE_RENDER, [$this->handlePut($id), $this->getRelations(), $visibleProperties, $hiddenProperties]);
                            break;
                        default:
                            throw new CHttpException(404, "Resource Not Found");
                    }
                }
            );
        }
    
        /**
         * handlePut
         *
         * Helper method for PUT actions
         *
         * @param (Mixed/Int) (id) unique identifier of the resource to put
         *
         * @return (Object) Returns the model of the updated resource
         */ 
        public function handlePut($id)
        {
            $model = $this->controller->emitRest(
                ERestEvent::MODEL_ATTACH_BEHAVIORS,
                $this->getModel($id)
            );
            $data = $this->controller->emitRest(ERestEvent::REQ_DATA_READ); 
            $restricted_properties = $this->controller->emitRest(ERestEvent::MODEL_RESTRICTED_PROPERTIES);
            $model = $this->controller->emitRest(ERestEvent::MODEL_APPLY_PUT_DATA, [$model, $data, $restricted_properties]);
            return $this->controller->emitRest(ERestEvent::MODEL_SAVE, [$model]);
        }
    
        /**
         * handlePutSubresource
         *
         * Helper method for PUT subresource actions
         *
         * @param (Mixed/Int) (id) unique identifier of the resource
         * @param (String) (subresource_name) name of the subresource
         * @param (Mixed/Int) (subresource_id) unique identifier of the subresource to put
         *
         * @return (Object) Returns the model containing the updated subresource
         */ 
        public function handlePutSubresource($id, $subresource_name, $subresource_id)
        {
            $model = $this->controller->emitRest(
                ERestEvent::MODEL_ATTACH_BEHAVIORS,
                $this->getModel($id)
            );
            $this->controller->emitRest(ERestEvent::MODEL_SUBRESOURCE_SAVE, [$model, $subresource_name, $subresource_id]);
            return $model;
        }
    }
    

    如果Laravel有这么优雅的代码,我立马把电脑吃了。来看看Laravel所谓的优雅代码:

    <?php
    class Blog_Controller extends Base_Controller
    {
        public function action_index()
        {
                // lets get our posts and eager load the
            // author
            $blogs = Blog::with('author')->order_by('created_at', 'desc')->paginate(20);
            // show the home view, and include our
            // posts too
            //$posts->appends(array('a' => 'app'));
            //print_r($blogs);exit;
            return View::make('blog.index')->with('blogs', $blogs);
        }
    
        public function action_add()
        {
            $method = Request::method();
            if($method =='POST'){
    
    
                // let's setup some rules for our new data
                // I'm sure you can come up with better ones
           $rules = array(
            'title'     => 'required|min:3|max:128',
            'content'       => 'required',  
            'file' => 'mimes:jpg,gif,png'   
            );
            //
            //Input::upload('picture', 'path/to/pictures', 'filename.ext');
            //File::cpdir($directory, $destination);
            //File::rmdir($directory);
            //echo File::mime('gif'); // outputs 'image/gif'
            //if (File::is('jpg', 'path/to/file.jpg'))
            //{
            //File::extension('picture.png');File::delete('path/to/file');
            //File::append('path/to/file', 'appended file content');
            //}File::put('path/to/file', 'file contents');$contents = File::get('path/to/file');
            // make the validator
                // let's get the new post from the POST data
                // this is much safer than using mass assignment
            $image = Input::file();
            $pictrue = '';
            $title = Input::get('title');
            $content = Input::get('content');
            $description = Input::get('title');
            if(empty($description)) $description = substr($content,0, 120);
            $author_id =Input::get('author_id');
            $tags =Input::get('tags');
            if(!empty($image['file']['name'])){
                $ext = File::extension($image['file']['name']);//$image['file']['tmp_name']
                Input::upload('file', path('public').'data', md5(date('YmdHis').$image['file']['name']).'.'.$ext);
                $pictrue = md5(date('YmdHis').$image['file']['name']).'.'.$ext;
            }
    
            $new_blog = array(
            'title'     => $title,
            'description'   => $description,
            'content'       => $content,
            'author_id' => $author_id,
            'views' => 0,
            'pictrue'   => $pictrue,
            'tags'   => $tags
                );
            $v = Validator::make($new_blog, $rules);
    
            if ( $v->fails() )
            {
                // redirect back to the form with
                // errors, input and our currently
                // logged in user
                return Redirect::to('blog/add')
                ->with('user', Auth::user())
                ->with_errors($v)
                ->with_input();
            }
    
    
            // create the new post
            $blog = new Blog($new_blog);
            $blog->save();
    
            // redirect to viewing our new post
            return Redirect::to('blog/view/'.$blog->id);
            }
            else{
                // get the current user
                $user = Auth::user();
    
                // show the create post form, and send
                // the current user to identify the post author
                return View::make('blog.add')->with('user', $user);
            }
    
    
        }
    
        public function action_edit($id)
        {
            $method = Request::method();
            $user = Auth::user();
    
            $blog = Blog::find($id);
            if($user->id != $blog->author->id)
            {
                return View::make('blog.view')
                ->with('blog', $blog);
            }
            if($method =='POST')
            {
    
                // let's setup some rules for our new data
                // I'm sure you can come up with better ones
           $rules = array(
            'title'     => 'required|min:3|max:128',
            'content'       => 'required',  
            'file' => 'mimes:jpg,gif,png'   
            );
            //
            //Input::upload('picture', 'path/to/pictures', 'filename.ext');
            //File::cpdir($directory, $destination);
            //File::rmdir($directory);
            //echo File::mime('gif'); // outputs 'image/gif'
            //if (File::is('jpg', 'path/to/file.jpg'))
            //{
            //File::extension('picture.png');File::delete('path/to/file');
            //File::append('path/to/file', 'appended file content');
            //}File::put('path/to/file', 'file contents');$contents = File::get('path/to/file');
            // make the validator
                // let's get the new post from the POST data
                // this is much safer than using mass assignment
            $image = Input::file();
            $pictrue = '';
            $title = Input::get('title');
            $content = Input::get('content');
            $description = Input::get('title');
            if(empty($description)) $description = substr($content,0, 120);
            $author_id =Input::get('author_id');
            $tags =Input::get('tags');
            if(!empty($image['file']['name'])){
                $ext = File::extension($image['file']['name']);//$image['file']['tmp_name']
                Input::upload('file', path('public').'data', md5(date('YmdHis').$image['file']['name']).'.'.$ext);
                $pictrue = md5(date('YmdHis').$image['file']['name']).'.'.$ext;
            }
    
            $new_blog = array(
            'title'     => $title,
            'description'   => $description,
            'content'       => $content,
            'author_id' => $author_id,
            'views' => 0,
            'pictrue'   => $pictrue,
            'tags'   => $tags
                );
            $v = Validator::make($new_blog, $rules);
    
            if ( $v->fails() )
            {
                // redirect back to the form with
                // errors, input and our currently
                // logged in user
                return Redirect::to('blog/add')
                ->with('user', Auth::user())
                ->with_errors($v)
                ->with_input();
            }
    
            $blog->title = $title;
            $blog->description = $description;
            $blog->content = $content;
            $blog->author_id = $author_id;
            $blog->tags = $tags;
            if(!empty($pictrue)) $blog->pictrue = $pictrue;
            $blog->save();
    
            // redirect to viewing our new post
            return Redirect::to('blog/view/'.$blog->id);
            }
            else
            {
    
                // show the full view, and pass the post
                // we just aquired
                return View::make('blog.edit')->with('blog', $blog);
            }
        }
    
        public function action_view($id)
        {
            // get our post, identified by the route
            // parameter
            $blog = Blog::find($id);
            $blog->views += 1;
            $blog->save();
            // show the full view, and pass the post
            // we just aquired
            return View::make('blog.view')
            ->with('blog', $blog);
        }
    
        public function action_delete($id)
        {
        }
    
        public function action_search()
        {
            return View::make('blog.search');
        }
    }
    

    屎一样的代码我不忍心看了。这他妈的就是面向过程的开发好么,话说这么多静态方法是啥意思?
    laravel之所以这么火,靠的是营销和吹牛逼。看看laravel的首页就知道,花花绿绿的。
    金玉其外,败絮其中。symfony不知道比laravel高了多少个等级,去看看symfony首页。

    laravel在github上面的提交更新好高啊!当然了,是个程序员都能提,当然高了。laravel里面的代码比起YII里面的代码。真的渣的跟屎一样。
    laravel用的人好多啊!因为laravel不需要费脑子,源代码一看就懂,自信心爆棚,觉得老子好聪明啊,全球最多人使用的框架,数百位工程师的结晶的源代码一看就懂(同时用几款框架的不在此列)
    再来看看laravel首页说的那些傻叉言论。什么Restfull,什么企业级,什么稳定,什么开源的。他妈的要是一个PHP框架连这些最基本的要求都达不到,还用PHP框架干什么?laravel把这些拿出来,就是骗一些脑残的程序员,殊不知这些要求都是好的PHP框架最基本的。
    Laravel更优雅,第一次看见laravel的时候惊呼:这不就是php版的rails吗?
    看到这句话我笑了。

    Laravel搞不好面向对象,就把操作弄成rails的形式,美其名曰,简洁。当然,我不得不承认Laravel还真他妈简洁。说实话有时候简洁快速真的很重要。就说ORM来说,YII生成一个model比起Laravel要复杂太多。
    YII是纯面向对象的,不会这些奇淫技巧。
    laravel的精髓在于ioc和facade 理解了思想就能更上一步。这他妈不是废话么?你把YII的Behavior理解了,你也能更上一步。而且IOC和facade这种东西就是奇淫技巧,没有一点用,耍花枪的真正上战场干不过耍大刀的。
    而且百度和傻叉腾讯内部PHP项目基本上都是YII(腾讯也大量用thinkphp,比如腾讯家居)。
    还有一点让某些程序员欲罢不能。laravel基本工作就是让你按照面向过程写代码,你需要的代码直接调用静态方法就行了。这就是为什么laravel会让程序员写代码感觉那么happy。面想过程起来真的很爽。
    YII一开始就让程序员面向对象,面向对象写起来当然一点也没有面向过程写起来那么happy。
    YII被广泛用于金融领域,试问哪个PHP框架可以做到?laravel的安全机制就是渣。经常被黑客们cookie伪造,解密。YII你来试试。

    最近有人说YII会被Symfony取代,我只能说他们想多了。
    YII被广泛用于那些对安全特性要求较高和稳定性要求较高的领域,比如OA系统(百度内部),百度商业基础平台,金融领域
    补充一下,小米的php部分也是YII开发的。
    多玩游戏也是YII开发的
    其实我不想说奇虎360也是YII

    腾讯大多数都是(与PHP相关的)

    兄台去智联招聘搜搜吧!
    搜YII几十页
    搜所谓的laravel好像只有一页
    去内推搜YII 会发现多如狗
    去内推搜laravel会发现好像一页都没有占满
    拉勾网laravel一个都搜不到
    所以说
    laravel玩玩可以,用来做企业级,我只能呵呵
    刺痛某些程序员的G点的来了!一般用laravel的,基本上都是外包公司,没有自己的核心产品。因为laravel开发快,做起来像那么回事

    回复
    0
  • PHP中文网

    PHP中文网2017-04-10 16:28:38

    <?php
        echo "hello,world";
    ?>

    回复
    0
  • 取消回复