CodeIgniter 4中的重定向

301 阅读1分钟

下载并安装CodeIgniter 4

下载最新版本的CodeIgniter 4,并将源代码解压到名为LearnCodeIgniter4WithRealApps的新文件夹。

公共文件夹中的index.phphtaccess文件剪切到项目的文件夹中。

打开文件夹中的index.php,找到第16行,将路径替换为Paths.php文件,如下所示。

$pathsPath = realpath(FCPATH . '/app/Config/Paths.php');

打开app/Config文件夹中的App.php,找到第39行,在$indexPage变量中删除index.php字符串,如下所示。

public $indexPage = '';

(adsbygoogle = window.adsbygoogle || []).push({})。

设置BASE URL

打开app/Config文件夹中的App.php文件。设置**$baseURL**变量的值,如下所示。

public $baseURL = 'http://localhost:8091/LearnCodeIgniter4WithRealApps/';

创建控制器

app/Controllers文件夹下创建名为Demo.php的新PHP文件,如下所示。

<?php

namespace App\Controllers;

class Demo extends BaseController
{

	public function __construct()
	{
		helper(['url']);
	}

	public function index()
	{
		return view('demo/index');
	}

	public function clickMe()
	{
		return $this->response->redirect(site_url('demo/index2'));
	}

	public function index2()
	{
		return view('demo/index2');
	}
}								

(adsbygoogle = window.adsbygoogle || []).push({})。

创建视图

app/Views文件夹下创建名为Demo的新文件夹。在这个文件夹中,创建新的PHP文件,命名如下。

索引视图

demo文件夹中,创建名为index.php的新的PHP文件,如下所示。

<html>

	<head>
		<title>Redirect in Codeigniter 4</title>
	</head>

	<body>

		<h3>Index</h3>
		<a href="<?= site_url('demo/clickMe') ?>">Click Me</a>
		
	</body>

</html>

(adsbygoogle = window.adsbygoogle || []).push({})。

索引2 视图

demo文件夹中,创建名为index2.php的新的PHP文件,如下所示。

<html>

	<head>
		<title>Redirect in Codeigniter 4</title>
	</head>

	<body>

		<h3>Index 2</h3>
		<a href="<?= site_url('demo/index') ?>">Back</a>

	</body>

</html>
			

(adsbygoogle = window.adsbygoogle || []).push({})。

定义路线

打开app/Config文件夹中的Routes.php文件,定义路线如下。

$routes->get('/', 'Demo::index');
$routes->get('/demo/index2', 'Demo::index2');
$routes->get('/demo/clickMe', 'Demo::clickMe');

CodeIgniter项目的结构

(adsbygoogle = window.adsbygoogle || []).push({})。

运行应用程序

用以下网址访问Demo控制器中的index动作**:http://localhost:8091/LearnCodeIgniter4WithRealApps/demo/index**

输出

The postRedirect in CodeIgniter 4appeared first onLearn Programming with Real Apps.