【问题标题】:How does instagram do username system without having get ID's like in PHPinstagram 如何在没有像 PHP 那样获取 ID 的情况下做用户名系统
【发布时间】:2017-11-20 07:04:21
【问题描述】:

我正在为大学建立一个小型社交网络,我希望用户名看起来像这样 http://www.example.com/username 就像 Instagram 那样,但我知道是这样的 http://www.example.com/profile?id=username 但我不希望这样 http://www.example.com/profile?id=username 我想要这样 http://www.example.com/username 。请问我该怎么做。

【问题讨论】:

  • Instagram 可能不使用 PHP。据说它是用 Python 和 Django 编写的。
  • 有多种方法和框架可以做到这一点。您要查找的术语是“干净的 URL”、“漂亮的 URL”、“SEO 友好的 URL”、“干净的查询字符串”等。
  • 你知道什么是url重写和路由器吗?

标签: php instagram


【解决方案1】:

每个 PHP 框架都支持Semantic urls(漂亮的 URL)。使用 Slim 框架,这很容易实现:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require __DIR__ . '/vendor/autoload.php';

$app = new \Slim\App;
$app->get('/{username}', function (Request $request, Response $response) {
    $username = $request->getAttribute('username');
    $response->getBody()->write("Hello, $username");

    return $response;
});
$app->run();

你可以从Slim Framework 3 Skeleton Application开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    相关资源
    最近更新 更多