>백엔드 개발 >PHP 튜토리얼 >ThinkPHP, ZF2, Yaf, Laravel 프레임워크 라우팅 경쟁_php 기술

ThinkPHP, ZF2, Yaf, Laravel 프레임워크 라우팅 경쟁_php 기술

PHP中文网
PHP中文网원래의
2016-05-16 20:19:151668검색

머리말

라우팅 구성을 소개하는 Zend Framework2 "ZF2 Multi-level Tree Routing Route Configuration 예제"에 대한 기술 기사를 읽었습니다. 저는 이것이 매우 흥미롭다고 생각합니다.

/user는 사용자 목록 페이지에 해당합니다.
/user/:user_id는 사용자의 개인 홈페이지에 해당합니다. AlloVince 사용자의 개인 홈페이지
/user/:user_id/blog/는 사용자의 블로그 목록 페이지에 해당합니다. 예를 들어 /user/AlloVince/blog는 AlloVince
/user/:user_id/blog/가 작성한 블로그를 나열합니다. :blog_id 사용자에 해당하는 기사 블로그 기사
구성표는 원문에서 인용되었습니다:

'router' => array(
  'routes' => array(
    'user' => array(
      'type' => 'Segment',
      'options' => array(
        'route' => '/user[/]',
        'defaults' => array(
          'controller' => 'UserController',
          'action' => 'index',
        ),
      ),
      'may_terminate' => true,
      'child_routes' => array(
        'profile' => array(
          'type' => 'Segment',
          'options' => array(
            'route' => '[:id][/]',
            'constraints' => array(
              'id' => '[a-zA-Z0-9_-]+'
            ),
            'defaults' => array(
              'action' => 'get'
            ),
          ),
          'may_terminate' => true,
          'child_routes' => array(
            'blog' => array(
              'type' => 'Segment',
              'options' => array(
                'route' => 'blog[/]',
                'constraints' => array(
                ),
                'defaults' => array(
                  'action' => 'blog'
                )
              ),
              'may_terminate' => true,
              'child_routes' => array(
                'post' => array(
                  'type' => 'Segment',
                  'options' => array(
                    'route' => '[:post_id][/]',
                    'constraints' => array(
                      'post_id' => '[a-zA-Z0-9_-]+'
                    ),
                    'defaults' => array(
                      'action' => 'post'
                    )
                  ),
                  'may_terminate' => true,
                ),
              ),
            ),
          ), //profile child_routes end
        ), //profile end
      ), //user child_routes end
    ), //user end
  ),
),

이 기사를 읽은 후 이 라우팅 요구 사항을 구현하는 데 사용한 PHP 프레임워크를 사용하세요.

ThinkPHP

새 ThinkPHP 프로젝트 만들기:

코드는 다음과 같습니다.

composer create-project topthink/thinkphp tp --prefer-dist

명령줄에 설치 내용이 표시됩니다.

topthink/thinkphp (3.2.2) 설치
ThinkPHP 공식 웹사이트의 최신 안정 버전은 3.2.3인 것으로 확인됩니다.

Packagist 공식 홈페이지에 가서 확인해 보니 라이브러리에 있는 안정 버전이 실제로 3.2.2였습니다.

3.2.3을 사용해야 합니다. 나는 왜 이것에 이렇게 집착하는 걸까? 이유:

3.2의 라우팅 기능은 모듈에 대해 설정되어 있으므로 URL의 모듈 이름을 라우팅할 수 없으며 라우팅 정의는 일반적으로 모듈 구성 파일에 배치됩니다. 버전 3.2.3에는 전역 경로 정의 지원이 추가되었으며 프로젝트의 공개 구성 파일에서 경로를 정의할 수 있습니다.
즉, 라우팅 재작성 부분은 컨트롤러와 액션 부분인데, Moudle은 여전히 ​​존재합니다.

home/user가 아닌 /user를 원합니다. (ThinkPHP의 기본 모듈은 Home, 'DEFAULT_MODULE' => 'Home'이며 수정 가능)

물론 이 문제는 .htaccess 파일을 수정하여 해결할 수도 있습니다. 하지만 3.2.3을 설치하기로 결정했습니다.

ThinkPHP 공식 홈페이지에서 최신 패키지를 다운로드하고 압축을 풀어주세요.

브라우저를 사용하여 프로젝트 항목 파일에 액세스하고 ThinkPHP가 자동으로 기본 애플리케이션 모듈 홈을 생성하도록 합니다.

공용 구성 파일 tpApplicationCommonConfconfig.php를 수정합니다.

<?php
return array(
  // 开启路由
  &#39;URL_ROUTER_ON&#39; => true,
  // URL访问模式,可选参数0、1、2、3,代表以下四种模式:
  // 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默认为PATHINFO 模式
  &#39;URL_MODEL&#39; => 2,
  // URL伪静态后缀设置,为空表示可以支持所有的静态后缀
  // 使用U函数生成URL时会不带后缀
  &#39;URL_HTML_SUFFIX&#39; => &#39;&#39;,
  // URL变量绑定到Action方法参数,默认为true
  &#39;URL_PARAMS_BIND&#39; => true,
  // URL变量绑定的类型 0 按变量名绑定 1 按变量顺序绑定,默认为0
  &#39;URL_PARAMS_BIND_TYPE&#39; => 0,
  // 路由配置
  &#39;URL_ROUTE_RULES&#39; => array(
    &#39;/^url$/&#39; => &#39;Home/User/url&#39;,
    &#39;/^user$/&#39; => &#39;Home/User/index&#39;,
    &#39;/^user\/([a-zA-Z0-9_-]+)$/&#39; => &#39;Home/User/show?name=:1&#39;,
    &#39;/^user\/([a-zA-Z0-9_-]+)\/blog$/&#39; => &#39;Home/Blog/index?name=:1&#39;,
    &#39;/^user\/([a-zA-Z0-9_-]+)\/blog\/([0-9]+)$/&#39; => &#39;Home/Blog/show?name=:1&blog_id=:2&#39;,
  ),
);
?>


tpApplicationHomeControllerUserController 파일을 생성합니다. class.php:

<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
  public function url() {
    $name = &#39;jing&#39;;
    $blogId = 1;
    $urls = array(
      U(&#39;/user&#39;),
      U("/user/{$name}"),
      U("/user/{$name}/blog"),
      U("/user/{$name}/blog/{$blogId}"),
    );
    foreach ($urls as $url) {
      echo "<a href=\"{$url}\">{$url}<a/><br />\n";
    }
  }
  public function index() {
    echo &#39;我是用户列表^_^&#39;;
  }
  public function show($name) {
    echo "欢迎你,{$name}";
  }
}
?>


tpApplicationHomeControllerBlogController.class.php 파일 생성:

<?php
namespace Home\Controller;
use Think\Controller;
class BlogController extends Controller {
  public function index($name) {
    echo "这是{$name}的博客列表";
  }
  public function show($blog_id, $name) {
    echo "{$name}的这篇博客的id为{$blog_id}";
  }
}
?>


액세스: http://127.0.0.1/tp/url

출력:

코드는 다음과 같습니다.

<a href="/tp/user">/tp/user<a/><br />
<a href="/tp/user/jing">/tp/user/jing<a/><br />
<a href="/tp/user/jing/blog">/tp/user/jing/blog<a/><br />
<a href="/tp/user/jing/blog/1">/tp/user/jing/blog/1<a/><br />

위 4개의 링크를 방문하여 차례로 돌아옵니다.

나는 사용자 목록입니다^_^
환영합니다, jing
This is jing
jing의 블로그 목록의 ID는 1입니다.
아래의 다른 프레임워크에서도 위 내용이 출력됩니다.

Zend Framework 2

ZF2 스켈레톤 프로그램을 사용하여 ZF2 프로젝트 생성:

composer create-project --stability="dev" zendframework/ Skeleton -application zf2

기본 모듈 애플리케이션의 구성 파일 zf2moduleApplicationconfigmodule.config.php를 수정합니다:

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link   http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd New BSD License
 */
return array(
  &#39;router&#39; => array(
    &#39;routes&#39; => array(
      &#39;home&#39; => array(
        &#39;type&#39; => &#39;Zend\Mvc\Router\Http\Literal&#39;,
        &#39;options&#39; => array(
          &#39;route&#39; => &#39;/url&#39;,
          &#39;defaults&#39; => array(
            &#39;controller&#39; => &#39;Application\Controller\User&#39;,
            &#39;action&#39; => &#39;url&#39;,
          ),
        ),
      ),
      // The following is a route to simplify getting started creating
      // new controllers and actions without needing to create a new
      // module. Simply drop new controllers in, and you can access them
      // using the path /application/:controller/:action
      &#39;application&#39; => array(
        &#39;type&#39; => &#39;Literal&#39;,
        &#39;options&#39; => array(
          &#39;route&#39; => &#39;/application&#39;,
          &#39;defaults&#39; => array(
            &#39;__NAMESPACE__&#39; => &#39;Application\Controller&#39;,
            &#39;controller&#39; => &#39;Index&#39;,
            &#39;action&#39; => &#39;index&#39;,
          ),
        ),
        &#39;may_terminate&#39; => true,
        &#39;child_routes&#39; => array(
          &#39;default&#39; => array(
            &#39;type&#39; => &#39;Segment&#39;,
            &#39;options&#39; => array(
              &#39;route&#39; => &#39;/[:controller[/:action]]&#39;,
              &#39;constraints&#39; => array(
                &#39;controller&#39; => &#39;[a-zA-Z][a-zA-Z0-9_-]*&#39;,
                &#39;action&#39; => &#39;[a-zA-Z][a-zA-Z0-9_-]*&#39;,
              ),
              &#39;defaults&#39; => array(
              ),
            ),
          ),
        ),
      ),
      &#39;user_list&#39; => array(
        &#39;type&#39; => &#39;Segment&#39;,
        &#39;options&#39; => array(
          &#39;route&#39; => &#39;/user[/]&#39;,
          &#39;defaults&#39; => array(
            &#39;__NAMESPACE__&#39; => &#39;Application\Controller&#39;,
            &#39;controller&#39; => &#39;User&#39;,
            &#39;action&#39; => &#39;index&#39;,
          ),
        ),
        &#39;may_terminate&#39; => true,
        &#39;child_routes&#39; => array(
          &#39;user&#39; => array(
            &#39;type&#39; => &#39;Segment&#39;,
            &#39;options&#39; => array(
              &#39;route&#39; => &#39;[:name][/]&#39;,
              &#39;constraints&#39; => array(
                &#39;name&#39; => &#39;[a-zA-Z0-9_-]+&#39;,
              ),
              &#39;defaults&#39; => array(
                &#39;action&#39; => &#39;show&#39;,
              ),
            ),
            &#39;may_terminate&#39; => true,
            &#39;child_routes&#39; => array(
              &#39;blog_list&#39; => array(
                &#39;type&#39; => &#39;Segment&#39;,
                &#39;options&#39; => array(
                  &#39;route&#39; => &#39;blog[/]&#39;,
                  &#39;constraints&#39; => array(
                  ),
                  &#39;defaults&#39; => array(
                    &#39;controller&#39; => &#39;Blog&#39;,
                    &#39;action&#39; => &#39;index&#39;,
                  )
                ),
                &#39;may_terminate&#39; => true,
                &#39;child_routes&#39; => array(
                  &#39;blog&#39; => array(
                    &#39;type&#39; => &#39;Segment&#39;,
                    &#39;options&#39; => array(
                      &#39;route&#39; => &#39;[:blog_id]&#39;,
                      &#39;constraints&#39; => array(
                        &#39;blog_id&#39; => &#39;[0-9]+&#39;,
                      ),
                      &#39;defaults&#39; => array(
                        &#39;action&#39; => &#39;show&#39;,
                      )
                    ),
                    &#39;may_terminate&#39; => true,
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  ),
  &#39;service_manager&#39; => array(
    &#39;abstract_factories&#39; => array(
      &#39;Zend\Cache\Service\StorageCacheAbstractServiceFactory&#39;,
      &#39;Zend\Log\LoggerAbstractServiceFactory&#39;,
    ),
    &#39;aliases&#39; => array(
      &#39;translator&#39; => &#39;MvcTranslator&#39;,
    ),
  ),
  &#39;translator&#39; => array(
    &#39;locale&#39; => &#39;en_US&#39;,
    &#39;translation_file_patterns&#39; => array(
      array(
        &#39;type&#39; => &#39;gettext&#39;,
        &#39;base_dir&#39; => __DIR__ . &#39;/../language&#39;,
        &#39;pattern&#39; => &#39;%s.mo&#39;,
      ),
    ),
  ),
  &#39;controllers&#39; => array(
    &#39;invokables&#39; => array(
      &#39;Application\Controller\Index&#39; => &#39;Application\Controller\IndexController&#39;,
      &#39;Application\Controller\User&#39; => &#39;Application\Controller\UserController&#39;,
      &#39;Application\Controller\Blog&#39; => &#39;Application\Controller\BlogController&#39;,
    ),
  ),
  &#39;view_manager&#39; => array(
    &#39;display_not_found_reason&#39; => true,
    &#39;display_exceptions&#39; => true,
    &#39;doctype&#39; => &#39;HTML5&#39;,
    &#39;not_found_template&#39; => &#39;error/404&#39;,
    &#39;exception_template&#39; => &#39;error/index&#39;,
    &#39;template_map&#39; => array(
      &#39;layout/layout&#39; => __DIR__ . &#39;/../view/layout/layout.phtml&#39;,
      &#39;application/index/index&#39; => __DIR__ . &#39;/../view/application/index/index.phtml&#39;,
      &#39;error/404&#39; => __DIR__ . &#39;/../view/error/404.phtml&#39;,
      &#39;error/index&#39; => __DIR__ . &#39;/../view/error/index.phtml&#39;,
    ),
    &#39;template_path_stack&#39; => array(
      __DIR__ . &#39;/../view&#39;,
    ),
  ),
  // Placeholder for console routes
  &#39;console&#39; => array(
    &#39;router&#39; => array(
      &#39;routes&#39; => array(
      ),
    ),
  ),
);
?>


스켈레톤 프로그램과 함께 제공되는 파일입니다. 라우터 부분과 컨트롤러 부분만 수정했습니다. 이렇게 긴 문서를 작성하는 것은 나에게 너무 어려울 것입니다. ZF가 공식적으로 스켈레톤 프로그램을 발표한 이유이기도 하다.

zf2moduleApplicationsrcApplicationControllerUserController.php 파일 생성:

<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class UserController extends AbstractActionController {
  public function urlAction() {
    $name = &#39;jing&#39;;
    $blogId = 1;
    $urls = array(
      $this->url()->fromRoute(&#39;user_list&#39;),
      $this->url()->fromRoute(&#39;user_list/user&#39;, array(&#39;name&#39; => $name)),
      $this->url()->fromRoute(&#39;user_list/user/blog_list&#39;, array(&#39;name&#39; => $name)),
      $this->url()->fromRoute(&#39;user_list/user/blog_list/blog&#39;, array(&#39;name&#39; => $name, &#39;blog_id&#39; => $blogId)),
    );
    $view = new ViewModel(compact(&#39;urls&#39;));
    $view->setTerminal(true);
    return $view;
  }
  public function indexAction() {
    $view = new ViewModel();
    // 禁用布局模板
    $view->setTerminal(true);
    return $view;
  }
  public function showAction() {
    $username = $this->params()->fromRoute(&#39;name&#39;);
    $view = new ViewModel(compact(&#39;username&#39;));
    $view->setTerminal(true);
    return $view;
  }
}
?>


zf2moduleApplicationsrcApplicationControllerBlogCon troller.php 파일 생성:

<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BlogController extends AbstractActionController {
  public function indexAction() {
    $username = $this->params()->fromRoute(&#39;name&#39;);
    $view = new ViewModel(compact(&#39;username&#39;));
    $view->setTerminal(true);
    return $view;
  }
  public function showAction() {
    $username = $this->params()->fromRoute(&#39;name&#39;);
    $blogId = $this->params()->fromRoute(&#39;blog_id&#39;);
    $view = new ViewModel(compact(&#39;username&#39;, &#39;blogId&#39;));
    $view->setTerminal(true);
    return $view;
  }
}
?>


zf2는 Action 매개변수 바인딩을 지원하지 않습니다. ThinkPHP는 바인딩뿐만 아니라 2가지 바인딩 방법도 지원합니다. 변수 이름으로 바인딩하고 변수 순서로 바인딩합니다.

exit()를 제외하고 zf2의 작업은 뷰로 돌아가야 합니다. 보기를 비활성화하는 방법을 알고 계시다면 알려주세요.

zf2moduleApplicationviewapplicationuserurl.phtml 파일 생성:

<?php foreach ($urls as $url): ?>
<a href="<?php echo $url;?>"><?php echo $url;?><a/><br />
<?php endforeach; ?>

zf2moduleApplicationviewapplicationuserindex.phtml 파일 생성:

나는 사용자 목록입니다^_^
파일 생성 zf2moduleApplicationviewapplicationusershow.phtml:

환영합니다. 720b36365a2096d271439392c84df50c
파일 생성 zf2moduleApplicationviewapplicationblogindex.phtml:

이것은 720b36365a2096d271439392c84df50c 블로그 목록
zf2moduleApplicationviewapplicationblogshow.phtml 파일 만들기:

<?php echo $username; ?>的这篇博客的id为<?php echo $blogId; ?>


Yaf

Yaf 설치

코드 생성 도구를 사용하여 Yaf 프로젝트 생성

시작 파일 yafapplicationBootstrap.php를 수정하고 그 안의 _initRoute 메소드를 수정합니다:

$router = Yaf_Dispatcher::getInstance()->getRouter();
    $route0 = new Yaf_Route_Rewrite(&#39;url&#39;, array(
      &#39;controller&#39; => &#39;User&#39;,
      &#39;action&#39; => &#39;url&#39;,
        ), array()
    );
    $route1 = new Yaf_Route_Rewrite(&#39;user&#39;, array(
      &#39;controller&#39; => &#39;User&#39;,
      &#39;action&#39; => &#39;index&#39;,
        ), array()
    );
    $route2 = new Yaf_Route_Regex(&#39;#user/([a-zA-Z0-9_-]+)#&#39;, array(
      &#39;controller&#39; => &#39;User&#39;,
      &#39;action&#39; => &#39;show&#39;,
        ), array(1 => &#39;name&#39;,)
    );
    $route3 = new Yaf_Route_Regex(&#39;#user/([a-zA-Z0-9_-]+)/blog#&#39;, array(
      &#39;controller&#39; => &#39;Blog&#39;,
      &#39;action&#39; => &#39;index&#39;,
        ), array(1 => &#39;name&#39;,)
    );
    $route4 = new Yaf_Route_Regex(&#39;#user/([a-zA-Z0-9_-]+)/blog/([0-9]+)#&#39;, array(
      &#39;controller&#39; => &#39;Blog&#39;,
      &#39;action&#39; => &#39;show&#39;,
        ), array(1 => &#39;name&#39;, 2 => &#39;blogId&#39;,)
    );
    $router->addRoute(&#39;url&#39;, $route0);
    $router->addRoute(&#39;user_list&#39;, $route1);
    $router->addRoute(&#39;user&#39;, $route2);
    $router->addRoute("blog_list", $route3);
    $router->addRoute("blog", $route4);

Yaf에는 라우팅 기능이 있지만 경로명을 기준으로 URL을 생성하는 방법은 없습니다. 그래서 URL을 연결하기 위한 프로젝트 이름을 정의했습니다.

구성 파일에 구성 항목 yafconfapplication.ini를 추가합니다.

project.name = &#39;yaf&#39;

yafapplicationcontrollersUser.php 파일을 생성합니다.

<?php
class UserController extends Yaf_Controller_Abstract {
  public function urlAction() {
    $name = &#39;jing&#39;;
    $blogId = 1;
    $app = Yaf_Application::app();
    $projectName = $app->getConfig()->project->name;
    $urls = array(
      "/{$projectName}/user",
      "/{$projectName}/user/{$name}",
      "/{$projectName}/user/{$name}/blog",
      "/{$projectName}/user/{$name}/blog/{$blogId}",
    );
    foreach ($urls as $url) {
      echo "<a href=\"{$url}\">{$url}<a/><br />\n";
    }
    return false;
  }
  public function indexAction() {
    echo &#39;我是用户列表^_^&#39;;
    // 禁用视图模板
    return false;
  }
  public function showAction($name) {
    echo "欢迎你,{$name}";
    return false;
  }
}

Create yafapplicationcontrollersBlog .php 파일:

코드는 다음과 같습니다.

<?php
class BlogController extends Yaf_Controller_Abstract {
    public function indexAction($name) {
        echo "这是{$name}的博客列表";
        return false;
    }
    public function showAction($blogId, $name) {
        echo "{$name}的这篇博客的id为{$blogId}";
        return false;
    }

Yaf의 Action은 변수 이름으로 바인딩되는 매개변수 바인딩을 지원합니다. $name 및 $blogId는 매개변수 순서에 관계없이 경로에 구성된 이름과 동일해야 합니다.

Laravel

新建Laravel项目:

composer create-project laravel/laravel --prefer-dist

清除合并文件。在目录laravel\vendor\下有个文件compiled.php,这个文件是为了减少IO提高框架性能,将很多类文件合并到一个文件中而生存的。在开发环境下,应该删除该文件,否则修改了一些文件发现没有效果,其实是因为文件已经合并缓存了。
清除命令:

php artisan clear-compiled

在生产环境中应该开启,以提升性能:

php artisan optimize --force

修改路由文件laravel\app\Http\routes.php:

<?php
Route::get(&#39;/url&#39;, array(&#39;uses&#39; => &#39;UserController@getUrl&#39;));
Route::get(&#39;/user&#39;, array(&#39;uses&#39; => &#39;UserController@getIndex&#39;));
Route::get(&#39;/user/{username}&#39;, array(&#39;uses&#39; => &#39;UserController@getShow&#39;));
Route::get(&#39;/user/{username}/blog&#39;, array(
    &#39;as&#39; => &#39;blog_list&#39;,
    &#39;uses&#39; => &#39;BlogController@getIndex&#39;,
));
Route::get(&#39;/user/{username}/blog/{blogId}&#39;, array(
    &#39;as&#39; => &#39;blog&#39;,
    &#39;uses&#39; => &#39;BlogController@getShow&#39;,
))->where(array(&#39;blogId&#39; => &#39;[0-9]+&#39;));

查看路由定义情况:

 代码如下:

php artisan route:list

输出:

代码如下:

+--------+----------+-------------------------------+-----------+----------------------------------------------+------------+
| Domain | Method   | URI                           | Name      | Action                                       | Middleware |
+--------+----------+-------------------------------+-----------+----------------------------------------------+------------+
|        | GET|HEAD | url                           |           | App\Http\Controllers\UserController@getUrl   |            |
|        | GET|HEAD | user                          |           | App\Http\Controllers\UserController@getIndex |            |
|        | GET|HEAD | user/{username}               |           | App\Http\Controllers\UserController@getShow  |            |
|        | GET|HEAD | user/{username}/blog          | blog_list | App\Http\Controllers\BlogController@getIndex |            |
|        | GET|HEAD | user/{username}/blog/{blogId} | blog      | App\Http\Controllers\BlogController@getShow  |            |
+--------+----------+-------------------------------+-----------+----------------------------------------------+------------+

定义路由变量全局模式,修改文件laravel\app\Providers\RouteServiceProvider.php中的boot方法:

代码如下:

    public function boot(Router $router) {       
     $router->pattern(&#39;username&#39;, &#39;[a-zA-Z0-9_-]+&#39;);
        parent::boot($router);    
        }

创建UserController控制器:

代码如下:

php artisan make:controller UserController

Laravel帮我们在laravel\app\Http\Controllers目录下创建了文件UserController.php,文件中已经为我们写好一部分骨架代码。修改文件laravel\app\Http\Controllers\UserController.php:

 代码如下:

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller {
    public function getUrl() {
        $name = &#39;jing&#39;;
        $blogId = 1;
        $urls = array(
            url(&#39;/user&#39;),
            action(
&#39;UserController@getShow&#39;
, array($name)),
            route(&#39;blog_list&#39;, array($name)),
            route(&#39;blog&#39;, array($name, $blogId)),
        );
        foreach ($urls as $url) {
            echo "
{$url}
\n";
        }
    }
    public function getIndex() {
        echo &#39;我是用户列表^_^&#39;;
    }
    public function getShow($name) {
        echo "欢迎你,{$name}";
    }
}


创建BlogController控制器:

代码如下:

php artisan make:controller BlogController

修改文件laravel\app\Http\Controllers\BlogController.php:

代码如下:

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class BlogController extends Controller {
    public function getIndex($name) {
        echo "这是{$name}的博客列表";
    }
    public function getShow($name, $blogId) {
        echo "{$name}的这篇博客的id为{$blogId}";
    }
}

Laravel的Action也支持参数绑定,是按变量顺序绑定的,和变量名无关。

后语

我是Laravel粉,但是我也没有想黑其他框架的意思,大家有兴趣也可以用自己熟悉的框架来实现这个小例子,写了记得@我,语言不限。

以上所述就是本文的全部内容了,希望大家能够喜欢。

请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!

以上就是ThinkPHP、ZF2、Yaf、Laravel框架路由大比拼_php技巧的内容,更多相关内容请关注PHP中文网(www.php.cn)!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.