Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silm路由php_Php_Mysql_Slim - Fatal编程技术网

Silm路由php

Silm路由php,php,mysql,slim,Php,Mysql,Slim,我已经在mySql中创建了一个数据库,并且有一个用于组合项目的页面。然而,当我试图通过id创建一个指向公文包项目的链接时,我总是会出错 我看了一下苗条的 我还看了这里关于stackoverflow的各种问题,包括: 这个 关于组路由,我还看了一些,但我仍然感到困惑。我在互联网上读到过关于使用PDO的内容,但还没有涉及到这一点。我在想,当点击模板时,是否有方法将公文包项目路由到模板,其中url将使用mysql表中行的id号 这是我的索引文件,如果你需要查看更多,请告诉我。 非常感谢 <?ph

我已经在mySql中创建了一个数据库,并且有一个用于组合项目的页面。然而,当我试图通过id创建一个指向公文包项目的链接时,我总是会出错

我看了一下苗条的

我还看了这里关于stackoverflow的各种问题,包括:

这个

关于组路由,我还看了一些,但我仍然感到困惑。我在互联网上读到过关于使用PDO的内容,但还没有涉及到这一点。我在想,当点击模板时,是否有方法将公文包项目路由到模板,其中url将使用mysql表中行的id号

这是我的索引文件,如果你需要查看更多,请告诉我。 非常感谢

<?php

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set ( "Europe/London" );


$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));


$view = $app->view();
$view->parserOptions = array(
    'debug' => true,

);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$dbc = mysqli_connect('localhost','root','pass','tom_db')
or die('Error Connecting to mysql server');

$query = "SELECT id  FROM documentaries";
$result = mysqli_query($dbc,$query);

while ($row = mysqli_fetch_assoc($result)){
        $data[] = $row["id"];
}



//what happens here is what will run with our object
$app->get('/', function() use($app){
  $app->render('about.twig');
});


//portfolio page
    $app->get('/portfolio', function() use ($app) {
        $app->render('portfolio.twig');
    });

    $app->get('portfolio/{id}', function ($id) {
        $app->render('portfolioitem.twig', array(
            'data' => $data
        ));
    });

$app->get('/contact', function() use($app){
  $app->render('contact.twig');
})->name('contact');

$app->run();


?>

样式表是相对于当前URL路径的,因此

/portfolio
,它将查找
/portfolio/main.css
。使用
/main.css
(注意开头的
/
)更新链接元素以包含站点URL,或使其相对于根目录

这同样适用于模板中的链接,
portfolio/{{data.id}
将尝试指向
/portfolio/portfolio/{{data.id}}
,再次将站点url添加到开头,或添加斜杠

如果使用SLIMV3,请考虑使用
或者,对于V2,使用,edit:刚刚注意到您已经在使用它了

它允许您命名您的路线,例如:

$app->get('/portfolio/:id', function ($id) use ($app) {
    ...

    $app->render('portfolioitem.twig', array(
            'data' => $data
        )
    );
})->name('portfolio.id');
然后使用引用模板中的URL

{{ urlFor('portfolio.id', {"id": 17 }) }}
这意味着,如果您的URL发生更改,所有引用它的链接都会同时自动更新

您还需要确保
.htaccess
文件配置正确。如中的Slim文档所示,它应该包含

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
问题2 您正在运行查询,该查询在
$data
中生成一个结果数组,但是您试图使用
{{data.id}}
访问第一个结果,首先,您需要使用


{%for data%}
,然后您将能够使用
{{{item.id}
。或者,从PHP中获取第一项,如
$data=$data[0]
,然后将其传递到模板中。

请添加您得到的错误,好吗?@davideastore感谢您的查看我编辑了我的问题。还包括您的模板文件。@davideastore抱歉,应该立即包括它们。它们现在包括在内。许多人表示感谢,因为这个问题毫无意义。重新阅读后,听起来你的路线是可行的;正如在“/portfolio/1”中一样,它的解析是正确的。但是您在生成/呈现链接时遇到问题。但是,没有生成公文包项目链接的示例。谢谢您的回答。但我还是会出错。关于包含站点url,由于我使用xampp,它不允许我访问。关于命名路线,它出现了错误;在第38行的“portfolio.twig”中呈现模板时引发了异常(“name:portfolio.id的命名路由未找到”),如果您使用的是
xampp
,请确保
httpd.conf
文件正确
AllowOverride所有这些仍然不起作用。我已将.htaccess从上面显示的更改为您所做的,并已将
httpd.conf
设置为
“无”
。下面是我目前指向porfolio/id的链接的样子<代码>”http://localhost:8080/portfolio/{{data.id}}
这是否正确?很抱歉,您现在可能对此感到厌烦。不,它说确保它没有设置为“无”。当您单击链接时,是否看到该URL?或者这就是它在模板中的显示方式?呈现的页面应该用实际id替换
{{data.id}
<!DOCTYPE html>
<html>
  <head>
    {% block head %}
      <meta charset="utf-8">
      <title>Tom Turner - Director of Photography</title>
      <link rel="stylesheet" href="css/normalize.css">
      <link href='https://fonts.googleapis.com/css?family=Changa+One|Open+Sans:400,400italic,700,700italic,800' rel='stylesheet' type='text/css'>
      <link rel="stylesheet" href="css/main.css">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
      <script type="text/javascript" src="js/jquery.mixitup.min.js"></script>
      <script type="text/javascript" src="js/main.js"></script>
      <script type="text/javascript" src="js/responsivemenu.js"></script>

    {% endblock head %}
  </head>

  <body>
    <header>
      <a href="index.html" id="logo">
        <h1>Tom Turner</h1>
        <h2>Director of Photography</h2>
      </a>
      <nav>
        {% block nav %}
          <ul>
            <li><a href="{{ baseUrl() }}" class="selected">About</a></li>
            <li><a href="{{ siteUrl('/portfolio') }}">Portfolio</a></li>
            <li><a href="{{ siteUrl('/clients') }}">Clients</a></li>
            <li><a href="{{ siteUrl('/teaching') }}">Teaching</a></li>
            <li><a href="{{ siteUrl('/contact') }}">Contact</a></li>
          </ul>
        {% endblock nav %}
      </nav>
    </header>

    <div id="wrapper">

      {% block content %}
      {% endblock content %}

    </div>

     <footer class="main-footer">
       {% block footer %}


        <div id="footer-notes">
          <p>Tom Turner - Director of Photography</p>
          <p>&copy; Tom Turner - All Rights Reserved</p>
        </div>
       <div id="mayur">
          <p>&copy; 2015 Website by <a href="https//:www.mayurpande.com">Mayur Pande</a></p>

        </div>

        <div class="social-media">
          <ul>

              <li><a href="mailto:tom@allritesreversed.co.uk"><img src="img/mail_circle.png" alt="Email Logo" /></a></li>
              <li><a href="https://www.facebook.com/tom.turner.397501?fref=ts"><img src="img/fbcircle.png" alt="Facebook Logo" /></a></li>
              <li><a href="https://vimeo.com/user6107855"><img src="img/vimeo_circle.png" alt="Vimeo Logo" /></a></li>
              <li><a href="https://twitter.com/intent/tweet?screen_name=mayurpandeuk"><img src="img/twitter_circle.png" alt="Twitter Logo" /></a></li>

            </ul>
          </div>
       {% endblock footer %}
      </footer>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
      <script type="text/javascript" src="js/jquery.mixitup.min.js"></script>
      <script type="text/javascript" src="js/main.js"></script>
      <script type="text/javascript" src="js/responsivemenu.js"></script>

  </body>
</html>
RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
$app->get('/portfolio/:id', function ($id) use ($app) {
    ...

    $app->render('portfolioitem.twig', array(
            'data' => $data
        )
    );
})->name('portfolio.id');
{{ urlFor('portfolio.id', {"id": 17 }) }}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]