张掖艺能网站建设,网页入口网站推广,开一个小公司需要多少钱,天津个人网站备案查询前言#xff1a; Codeigniter 是PHP语言上比较普遍的MVC框架(不懂MVC的请出门左转#xff0c;那里有教程)。相对而言比较简单#xff0c;效率较高#xff08;一般认为仅次于Phalcon#xff09;#xff0c;市场占有率比较高#xff08;一般认为仅次于laravel#xff09;…前言 Codeigniter 是PHP语言上比较普遍的MVC框架(不懂MVC的请出门左转那里有教程)。相对而言比较简单效率较高一般认为仅次于Phalcon市场占有率比较高一般认为仅次于laravel。缺点是Codeigniter更新不够快新的内容不够多比如csrf等内容就曾经长期没有获得更新。
0.WAMP 注意codeigniter 4.0.3 需要至少PHP 7.2的平台。这里选择Wamp安装包。具体安装请参考其他教程。安装后的效果如下 1.安装Codeigniter 4 visual studio code 与laravel等需要composer的不同codeigniter 相对比较简单下载到www即可。 首先修改文件夹名称我修改为ci403 打开浏览器输入http://localhost/ci403/public/效果如下 这里其实是访问http://localhost/ci403/public/index.php而其他othercontroller以及对应的方法othermethod的参数otherparam访问都是 http://localhost/ci403/public/index.php/othercontroller/othermothod/otherparam 还有composer进行安装的方法这里略过。 安装visual studio code(略)。 打开ci403的文件夹内容如下。 现阶段不需要关注其结构只需要知道url路径找的public的内容即可 1.另一种启动方式 app/config/App.php有如下语句 public $baseURL http://localhost:8080/;打开ci403的目录命令行输入php spark serve 然后进入localhost:8080即可。这里略过。
2.修改base_url public $baseURL http://localhost/ci403/public/;备注路由默认的启动程序相关的都在app/Routers.php中比如 $routes-setDefaultNamespace(App\Controllers);
$routes-setDefaultController(Home);
$routes-setDefaultMethod(index);
$routes-setTranslateURIDashes(false);
$routes-set404Override();
$routes-setAutoRoute(true);$routes-get(/, Home::index);
//$routes-get(/site, Home::site);
这里的uri的写法是index.php/site去映射Home控制器的sitefunction。
3.helloworld的非正规创建 这里指明了默认的HomeController以及默认的方法。因此我们基于这一点创建helloworld程序。修改app/Controllers/Home.php如下
?php namespace App\Controllers;class Home extends BaseController
{public function index(){echo h1Hello Codeigniter 4.0.3h1;}
}效果如下 进一步修改app/config/Routers.php
$routes-get(/, Home::hello);修改app/Controllers/Home.php
?php namespace App\Controllers;class Home extends BaseController
{public function index(){// return view(welcome_message);echo h1Hello Codeigniter 4.0.3h1;}//--------------------------------------------------------------------public function hello(){echo h1Hello codeigniter 4-0-3h1;}
} 创建一个新的默认Controller修改app/Config/Routers.php $routes-setDefaultNamespace(App\Controllers);
$routes-setDefaultController(MyHome);
$routes-setDefaultMethod(index);
$routes-setTranslateURIDashes(false);
$routes-set404Override();
$routes-setAutoRoute(true);$routes-get(/, MyHome::index);然后创建app/Controllers/MyHome.php
?php namespace App\Controllers;class MyHome extends BaseController
{public function index(){// return view(welcome_message);echo h1Hello My NEW HOME Codeigniter 4.0.3h1;}//--------------------------------------------------------------------public function hello(string $name){echo h1Hello codeigniter 4-0-3 from h1.$name;}
}
效果如下 3通过url输入参数给方法 以及手动访问MyHome的hello方法同时传进去一个参数you。 http://localhost/ci403/public/index.php/MyHome/hello/you 4.Hellworld的正规创建通过view app/Controllers/MyHome.php
?php namespace App\Controllers;class MyHome extends BaseController
{public function index(){// return view(welcome_message);// echo h1Hello My NEW HOME Codeigniter 4.0.3h1;return view(myhello);}//--------------------------------------------------------------------public function hello(string $name){echo h1Hello codeigniter 4-0-3 from h1.$name;}
}
app/Views/myhello.php
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title
/head
bodyMy Hello world
/body
/html效果如下 5.controller传递参数给view app/views/myhello.php
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title
/head
body
My Hello world from ?php echo var_dump($data);?/body
/htmlapp/Controllers/MyHome.php
?php namespace App\Controllers;class MyHome extends BaseController
{public function index(){$mydata[data] [1,2,3];return view(myhello,$mydata);}public function hello(string $nameyyyy){$myname[data] array(one2,two$name,);return view(myhello,$myname);}
}效果如下 最后给一个url传递数据的例子 http://localhost/ci403/public/index.php/MyHome/hello/zzzz 再补充一个小的写法, 使用到了compact函数来把多个独立的变量进行打包发送。
?php namespace App\Controllers;class MyHome extends BaseController
{public function index(){$mydata[data] [1,2,3];return view(myhello,$mydata);}public function hello(string $nameyyyy){$myname[data] array(one2,two$name,);return view(myhello,$myname);}public function hello(){$nameyy;$email yyyy.com;$age 30;return view(myhello,compact($name, $email, $age));}
}齐活。