zoukankan      html  css  js  c++  java
  • Routes

    Routes

    Routing lets you create your own URL paths, based on the path you can load a closure or a controller.

    Routing Set-up

    Namespaces are included in all classes now. A namespace is like a layer, adding a namespace to a class means there can be multiple classes with the same name as long as each class is in a different namespace.

    With routes the namespace is RoutingRouter:: followed by the method call, typing out the namespace every time is long winded, thankfully short cuts can be created by creating an alias:

    use RoutingRouter;

    By using the use keyword RoutingRouter, it can be referenced as Router.

    To define a route, call the static name Router:: followed by either a post or a get ('any' can also be used to match both post and get requests) to match the HTTP action. Next, set the path to match and call a closure or a controller.

    Router::any('', 'closure or controller');

    Closures

    A closure is a function without a name, they are useful when you only need simple logic for a route, to use a closure first call Router:: then set the URL pattern you want to match against, followed by a function.

    Router::get('simple', function() { 
      //do something simple
    });

    Controllers and Models can also be used in a closure by instantiating the root controller.

    $c = new AppCoreController();
    $m = new AppModelsUsers(); 
    
    $m->getUsers();

    Having said that it's best to use a controller, if you need access to a model.

    Closures are convenient but can soon become messy.

    Controllers

    To call a route to a controller, instead of typing a function you can enter a string. In the string type the namespace of the controller (App/Controllers if located in the root of the controllers folder) then the controller name. Finally, specify what method of that class you wish to load. They are dictated by an '@'symbol.

    For example, to have a controller called Users (in the root of the controllers folder) and to load ausersList method, you would use the following:

    Router::get('users', 'AppControllersUsers@usersList');

    The above would call the Users controller and the userList method when /users is located in the URL, via a get request.

    Routes can respond to both GET and POST requests.

    To use a post route:

    Router::post('blogsave', 'AppControllersBlog@savePost');

    To respond to either a post or get request, use any:

    Router::any('blogsave', 'AppControllersBlog@savePost');

    Groups

    Group routes are new to 3.0. Routes can now be placed in a group, which allows all routes within the group to inherit the group name.

    Router::group('admin', function() {
        Router::any('add', 'AppControllersDemo@cool');
        Router::any('settings', 'AppControllersDemo@nice');
    });

    Is the equivalent to

    Router::any('admin/add', 'AppControllersAdmin@add');
    Router::any('admin/settings', 'AppControllersAdmin@settings');

    Group Prefixes and Namespaces

    The Router::group() can also accept an array as the first parameter and permit commands like:

    Router::group(['prefix' => 'admin', 'namespace' => 'AppControllersAdmin'], function() {
        Router::match('get',            'users',             'Users@index');
        Router::match('get',            'users/create',      'Users@create');
        Router::match('post',           'users',             'Users@store');
        Router::match('get',            'users/(:any)',      'Users@show');
        Router::match('get',            'users/(:any)/edit', 'Users@edit');
        Router::match(['put', 'patch'], 'users/(:any)',      'Users@update');
        Router::match('delete',         'users/(:any)',      'Users@destroy');
    });

    Where the prefix admin will turn the route users/create into admin/users/create and the namespaceAppControllersAdmin will prepend onto Users@create, turning intoAppControllersAdminUsers@create

    Router::resource()

    The Router::resource() method introduces the ability to write the group of resourceful routes, with the following specifications:

    HTTP MethodRouteController Method
    GET /photo index
    GET /photo/create create
    POST /photo store
    GET /photo/(:any) show
    GET /photo/(:any)/edit edit
    PUT/PATCH /photo/(:any) update
    DELETE /photo/(:any) destroy

    The previous code snippet can now be written as:

    Router::group(['prefix' => 'admin', 'namespace' => 'AppControllersAdmin'], function() {
        Router::resource('users', 'Users');
        Router::resource('categories', 'Categories');
        Router::resource('articles', 'Articles');
    });

    OR

    Router::resource('admin/users', 'AppControllersAdminUsers');
    Router::resource('admin/categories', 'AppControllersAdminCategories');
    Router::resource('admin/articles', 'AppControllersAdminArticles');

    Routing Filters

    Routes can also use filters to dynamically pass values to the controller / closure, there are 3 filters:

    1. (:any) any - can use characters or numbers
    2. (:num) num - can only use numbers
    3. (:all) all - will accept everything including any slash paths

    To use a filter place the filter inside parenthesis and use a colon inside route path.

    Router::get('blog/(:any)', 'AppControllersBlog@post');

    Would get past to app/Controllers/Blog.php anything after blog/ will be passed to post method.

    public function post($slug)
    {
        // Some code ...
    }

    Optional Parameters

    New to 3.0 is allowing filters to be optional

    Filters which are written like (:any) are required to match the route but writing a filter as (/(:any))makes it optional.

    This route supplied with Nova has one filter that is required then a further 3 optional filters. Multiple filters should be inside the first parenthesis.

    Router::any('admin/(:any)(/(:any)(/(:any)(/(:any))))', 'AppControllersDemo@test');

    Full Example

    use RoutingRouter;
    
    //define routes
    Router::get('', 'AppControllersWelcome@index');
    
    //call a controller in called users inside a admin folder inside the controllers folder
    Router::('admin/users', 'AppControllersAdminUsers@list');
  • 相关阅读:
    SQL Server 2005中的分区表(六):将已分区表转换成普通表
    关于SQL Server中分区表的文件与文件组的删除(转)
    MySQL修改root密码的几种方法
    Aptana 插件 for Eclipse 4.4
    IT励志与指导文章合集(链接)
    正则表达式(转)
    《疯狂原始人》温馨而搞笑片段截图
    指针函数与函数指针的区别(转)
    Linux内核@系统组成与内核配置编译
    2015年我国IT行业发展趋势分析(转)
  • 原文地址:https://www.cnblogs.com/chunguang/p/5642959.html
Copyright © 2011-2022 走看看