简体   繁体   中英

Dont know how to load page dynamically with Swoole

Hello developper friends.

I'm trying to make old website working with swolle http server but i'm stacking. I follow starting tutorial but can't find way to go head.

<?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$server = new Swoole\HTTP\Server("127.0.0.1", 9501);

$server->on("start", function (Server $server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$server->on("request", function (Request $request, Response $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$server->start();

Now i want to dynamically load my content (like what i use to do with apache server) according to the url but can't find how to do this. This is my old index file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="shortcut icon" href="images/logo.png" type="image/x-icon">
    <link rel='stylesheet' type='text/css' href='css/bootstrap.min.css' />
    <link rel='stylesheet' type='text/css' href='css/jquery-ui.css' />
    <link rel='stylesheet' type='text/css' href='css/font-awesome.min.css' />
    <link rel='stylesheet' type='text/css' href='css/bootstrap-select.min.css' />
    <link rel='stylesheet' type='text/css' href='css/select2.css' />
    <link rel='stylesheet' type='text/css' href='css/admin.css' />
    <script src='js/jquery-3.1.1.min.js'></script>
    <script src="js/fusioncharts.js"></script>
    <script src="js/fusioncharts.charts.js"></script>
    <script src="js/fusioncharts.theme.fusion.js"></script>
</head>
<body >
    <?php
        if(in_array($_GET['_page'],array('connexion','deconnexion')))
            require_once('vues/'.$_GET['_page'].'.php');
        else
        {
            require_once('vues/header.php');
            echo'<div class="container clearfix">
                    <div class="col-sm-2"><div class="menu-gauche">'; require_once('vues/menu.php');echo'</div></div>
                    <div class="col-sm-9 menu-droite">';
                    if(!isset($_GET['_act'])&&!in_array($_GET['_page'],array('accueil','compte','profil')))
                        include('vues/filtre.php');
                    require_once('vues/'.$_GET['_page'].'.php');
            echo'   </div>
                </div><div class="clearfix"></div>';
                require_once('vues/footer.php');
        }
    ?>
    <script src='js/mask-js.js'></script>
    <script src='js/bootstrap.min.js'></script>
    <script src='js/bootstrap-select.js'></script>
    <script src='js/general-js.js'></script>
    <script src="js/select2.js"></script>
    <script>
        $(document).ready(function(e){
                $(".toUp").on("keyup change",function(e){$(this).val(this.value.toUpperCase());});
                $('select').select2({
                    placeholder: "Select a State",
                    allowClear: true
                });
            });
    </script>
</body>
</html>

The content of the page is loaded according the call url.

Please i need to know how to overcome this.

Thanks to all

What you want to say basically is - how implement routing?. There are a few methods and properties available on request object like $request->get and $request->header that you might use to inspect what url user has entered in browser.

Combine these with if else and you can push your desired html to browser based on the url.

This is fine for your need if there aren't many pages that you need to worry about. If you have some demanding scenario you may have a look at easyswoole which provides an easy to use baked framework. There are already adapters available for Codeigniter or Laravel incase that is what you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM