ECN>> ci->> 返回
项目作者: AndiKod

项目描述 :
SublimeText Snippets for CodeIgniter4
高级语言:
项目地址: git://github.com/AndiKod/ci-.git
创建时间: 2019-11-05T12:51:52Z
项目社区:https://github.com/AndiKod/ci-

开源协议:

下载




SublimeCI





SublimeSnippets for CodeIgniter4



CI4
CI4

That project was started for my own needs, but some real documentation will follow and the rest of the usefull tags.

Install


Sublime Text - Package Control or Clone/Download

When i’ll add more snippets, the repo will be proposed as a Sublime Package, but until then you can clone or download it and place the folder inside ‘SublimeText3\Packages\User‘ folder.

ci- here we go…

Code Renders Tip
[ci-m]+Tab Model Have inside the basics for db connection
[ci-c]+Tab Controller With data loading from the model and view rendering
[ci-l]+Tab Layout Naked skeleton, to work with the view below
[ci-v]+Tab Vue Extending the layout above ^^
—- - Controllers&Routing - —-
—- - inside controllers - —-
[ci-hw]+Tab HelloWorld Controller The most basic controller, as a starting point.
[ci-pubF]+Tab public function For the methods within Controllers, and more
[ci-ptcF]+Tab protected function For the private logic. See Controllers doc.
[ci-tReq]+Tab $this->request
[ci-tRsp]+Tab $this->response
[ci-tLog]+Tab $this->logger
[ci-load-helpers]+Tab Load needed helpers Better to load them once in a BaseController
[ci-new-Model]+Tab Create the instance… Can be done if we first ci-use-Model
[ci-get-Model]+Tab …grab the Data. Create the array with the data for the ‘loop’
[ci-isFile-thEx]+Tab Check if file exist If not, throw exception. Done before load views.
[ci-data]+Tab Creates the $data array The strings and arrays to be sent into the Views
[ci-load-Parser]+Tab Makes the Parser available Captain Obvious striked again.
[ci-load-PView]+Tab Renders a ‘parsed’ View Where we echo variables as {title}
[ci-echo-View]+Tab Renders a PHP View Where we echo variables as <?= $title ?>
—- - filters - —-
[ci-filter]+Tab Filter file skeleton *For adding filter ‘before’ and or ‘after the controller*
—- - routing - —-
[ci-routesGrp]+Tab route Group Nesting routes that share the same initial segment.
[ci-routes]+Tab simple route Like the ‘about’ route, loading the ‘About’ Controller
[ci-routesSeg]+Tab (:segment) route Great for single posts. Match first segment.
[ci-RoutesAny]+Tab (:any) route Will match everything from the URI
[ci-RoutesNum]+Tab (:num) route When expecting a numeric segment like xByID/$id
[ci-RoutesAlpha]+Tab (:alpha) route No numbers
[ci-RoutesAlNum]+Tab (:alphanum) route Letters and/or numbers
[ci-RoutesHash]+Tab (:hash) route See the Models Docs for the usage
—- - Build Responses - —-
[ci-list-errors]+Tab Show validation errors Often placed right above the Form
[ci-loop]+Tab Looping trough arrays That’s the PHP version with foreach and /foreach
[ci-pl]+Tab Parsed Loop Just a quick {foo}{bar}{/foo} to display array elements like news
[ci-e]+Tab <?= $foo ?> Echo out a variable sent by the controller within the $data array
[ci-et]+Tab <?= ?> Just the empty ‘EchoTag’ in its short version
[ci-var]+Tab $foo[‘bar’] Use that inside ci-loop to print the variables from the result
—- - Some Basics - —-
[ci-t]+Tab $this-> …preaty self explanatory.

[ci-m]+Tab Model File

Usual models location: App/Models/

The meaning of the config settings are in the docs at CI4 Docs> Modeling Data> Using CodeIgniter’s Model.

The exemple function is from the official “News section” tutorial.

Tab steps:

  • ${1:App} : In case you changed your app namespace.
  • ${2:News} : The name of your Model file (always uppercase first letter).
  • ${3:news} : The corresponding database table (lowercase).
  • ${4:id} : In case you changed the primary_key
  • ${0} : The mouse cursor will end here after last tab.
  1. <?php namespace ${1:App}\Models;
  2. use CodeIgniter\Model;
  3. class ${2:News}Model extends Model
  4. {
  5. protected \$table = '${3:news}';
  6. protected \$primaryKey = '${4:id}';
  7. protected $returnType = 'array';
  8. protected $useSoftDeletes = false;
  9. protected \$allowedFields = ['title', 'slug'];
  10. // protected $useTimestamps = false;
  11. // protected $createdField = 'created_at';
  12. // protected $updatedField = 'updated_at';
  13. // protected $deletedField = 'deleted_at';
  14. // protected $validationRules = [];
  15. // protected $validationMessages = [];
  16. // protected $skipValidation = false;
  17. // Exemple function to retrive data
  18. public function get${2:News}(\$slug = false)
  19. {
  20. if (\$slug === false)
  21. {
  22. return \$this->orderBy('id', 'desc')->findAll(); // Get all records
  23. }
  24. return \$this->asArray()
  25. ->where(['slug' => \$slug])
  26. ->first();
  27. ${0} // or Get one record
  28. }
  29. }

To follow along with the official tutorial, create the news table as follow:

Within phpMyAdmin

  1. CREATE TABLE news (
  2. id int(11) NOT NULL AUTO_INCREMENT,
  3. title varchar(128) NOT NULL,
  4. slug varchar(128) NOT NULL,
  5. body text NOT NULL,
  6. PRIMARY KEY (id),
  7. KEY slug (slug)
  8. );
  9. INSERT INTO news VALUES
  10. (1,'Elvis sighted','elvis-sighted','Elvis was sighted at the Podunk internet cafe. It looked like he was writing a CodeIgniter app.'),
  11. (2,'Say it isn\'t so!','say-it-isnt-so','Scientists conclude that some programmers have a sense of humor.'),
  12. (3,'Caffeination, Yes!','caffeination-yes','World\'s largest coffee shop open onsite nested coffee shop for staff only.');

In the .env file from CI root folder:

  1. database.default.hostname = localhost
  2. database.default.database = ci4tutorial
  3. database.default.username = root
  4. database.default.password = root
  5. database.default.DBDriver = MySQLi

… that will create a basic news table.


[ci-c]+Tab Controller File

Usual controllers location: App/Controllers/

Keeped (commented) the basic stuff needed to get data from a Model,
the exemple works with the News model from the official tutorial.

Read the Controllers Doc for more details.

Tab steps:

  • ${1:App} : In case you changed your app namespace.
  • ${2:News} : The name of your Controller file (always uppercase first letter).
  • ${3:news} : The corresponding database table (lowercase).
  • ${4:id} : In case you changed the primary_key
  • ${0} : The mouse cursor will end here after last tab.
  1. <?php namespace ${1:App}\Controllers;
  2. use CodeIgniter\Controller;
  3. //use App\Models\NewsModel;
  4. class ${2:News} extends Controller
  5. {
  6. public function index()
  7. {
  8. //\$model = new NewsModel();
  9. \$data = [
  10. //'news' => $model->getNews(),
  11. 'title' => 'Some title',
  12. ];
  13. echo view('${3:news}/${4:index}', $data);
  14. }
  15. }

[ci-l]+Tab Layouts

Usual layouts location: App/Views/Layouts/

Basically an HTML skeleton with at least one ‘section’ inside,
acting like a placeholder into the views extended from the layout.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>Layout</title>
  6. </head>
  7. <body>
  8. <?= $this->renderSection('content') ?>
  9. </body>
  10. </html>

[ci-v]+Tab View

Snippet file: CI4-Snippets/ci4-view.sublime-snippet

Usual models location: App/Views/

Filling up the sections defined into the layout.

  1. <?= \$this->extend('${1:layouts/main}') ?>
  2. <?= \$this->section('${2:content}') ?>
  3. ${3:content goes here}
  4. <?= \$this->endSection() ?>

License

Copyright 2019, Andrei Curelaru andrei@andikod.fr

MIT