这是我的 DefaultController.php 文件我想在登录仪表板页面时获取仪表板上firstname的数据。但我得到'变量'这个名字“不存在。”错误,我是symfony的新手,我知道代码非常复杂,任何人都可以帮助我,我最近两天坚持这个任务。
DefaultController.php
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\users;
use AppBundle\Entity\Login;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$session = $request->getSession();
$user_id = $session->get('user_id');
if(isset($user_id) && $user_id != ''){
return $this->render('default/index.html.twig');
}else{
return $this->redirectToRoute('login');
}
}
/**
* @Route("/blog", name="blog")
*/
public function blogAction(Request $request)
{
return $this->render('default/blog.html.twig');
}
/**
* @Route("/signup", name="signup")
*/
public function signupAction(Request $request)
{
return $this->render('default/signup.html.twig');
}
/**
* @Route("/bootstrap_elements", name="bootstrap_elements")
*/
public function bootstrap_elementsAction(Request $request)
{
return $this->render('default/bootstrap_elements.html.twig');
}
/**
* @Route("/bootstrap_grid", name="bootstrap_grid")
*/
public function bootstrap_gridAction(Request $request)
{
return $this->render('default/bootstrap_grid.html.twig');
}
/**
* @Route("/forms", name="forms")
*/
public function formsAction(Request $request)
{
return $this->render('default/forms.html.twig');
}
/**
* @Route("/portfolio", name="portfolio")
*/
public function portfolioAction(Request $request)
{
return $this->render('default/portfolio.html.twig');
}
/**
* @Route("/register", name="register")
*/
public function registerAction(Request $request)
{
if($request->getMethod()=='POST'){
$username=$request->get('username');
$firstname=$request->get('firstname');
$password=$request->get('password');
$user = new Users();
$user->setUserName($username);
$user->setFirstName($firstname);
$user->setPassword($password);
$em=$this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush();
}
return $this->render('default/register.html.twig');
}
/**
* @Route("/timeline", name="timeline")
*/
public function timelineAction(Request $request)
{
return $this->render('default/timeline.html.twig');
}
/**
* @Route("/typography", name="typography")
*/
public function typographyAction(Request $request)
{
return $this->render('default/typography.html.twig');
}
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$session = $request->getSession();
$user_id = $session->get('user_id');
if(isset($user_id) && $user_id != ''){
return $this->redirectToRoute('homepage');
}else{
$em=$this->getDoctrine()->getEntityManager();
$repository=$em->getRepository('AppBundle:users');
if($request->getMethod()=='POST')
{
$this->get('session')->clear();
$username=$request->get('username');
$password=$request->get('password');
$remember=$request->get('remember');
$user=$repository->findOneBy(array('userName'=>$username, 'password'=>$password));
if($user)
{
/* echo "<pre>";
print_r($user);
exit; */
$session->set('user_id', $user->getId());
$session->set('name', $user->getFirstName());
/* if($remember=='remember-me')
{
$login=new Login();
$login->setUserName($username);
$login->setPassword($password);
$session->set('login', $login);
} */
// return $this->render('default/index.html.twig');
return $this->render('default/index.html.twig', array('name' => $user->getFirstName() ));
}
else
{
return $this->render('default/login.html.twig', array('name' => 'Login Error'));
}
}
else{
if ($session->has('login'))
{
$login=$session->get('login');
$username=$login->getUserName();
$password=$login->getPassword();
$user=$repository->findOneBy(array('userName'=>$username, 'password'=>$password));
if($user)
{
return $this->render('default/index.html.twig');
}
}
return $this->render('default/login.html.twig');
}
}
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction(Request $request)
{
$session = $request->getSession();
$this->get('session')->clear();
return $this->redirectToRoute('login');
}
}
这是我的 base.html.twig 在这个文件中我已经包含了我在所有文件中使用的标题和侧边栏。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - Deep Blue Admin</title>
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('templates/bootstrap/css/bootstrap.min.css') }}" />
<link rel="stylesheet" href="{{ asset('templates/font-awesome/css/font-awesome.min.css') }}" />
<link rel="stylesheet" href="{{ asset('templates/css/local.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ asset('http://www.shieldui.com/shared/components/latest/css/light-bootstrap/all.min.css') }}" />
<link rel="stylesheet" href="{{ asset('templates/css/2.css') }}">
{% endblock %}
{% block javascripts %}
<script src="{{asset('templates/js/jquery-1.10.2.min.js') }}"></script>
<script src="{{ asset('templates/bootstrap/js/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('http://www.shieldui.com/shared/components/latest/js/shieldui-all.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('http://www.prepbootstrap.com/Content/js/gridData.js') }}"></script>
<!-- you need to include the shieldui css and js assets in order for the charts to work -->
</head>
<body>
<div id="wrapper">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="">Admin Panel</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active"><a href=""><i class="fa fa-bullseye"></i> Dashboard</a></li>
<li><a href="{{ ('portfolio') }}"><i class="fa fa-tasks"></i> Portfolio</a></li>
<li><a href="{{ ('blog') }}"><i class="fa fa-globe"></i> Blog</a></li>
<li><a href="{{ ('signup') }}"><i class="fa fa-list-ol"></i> SignUp</a></li>
<li><a href="{{ ('register') }}"><i class="fa fa-font"></i> Register</a></li>
<li><a href="{{ ('timeline') }}"><i class="fa fa-font"></i> Timeline</a></li>
<li><a href="{{ ('forms') }}"><i class="fa fa-list-ol"></i> Add Product</a></li>
<li><a href="{{ ('typography') }}"><i class="fa fa-font"></i> Typography</a></li>
<li><a href="{{ ('bootstrap_elements') }}"><i class="fa fa-list-ul"></i> Bootstrap Elements</a></li>
<li><a href="{{ ('bootstrap_grid') }}"><i class="fa fa-table"></i > Bootstrap Grid</a></li>
</ul>
<ul class="nav navbar-nav navbar-right navbar-user">
<li class="dropdown messages-dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-envelope"></i> Messages <span class="badge">2</span> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">2 New Messages</li>
<li class="message-preview">
<a href="#">
<span class="avatar"><i class="fa fa-bell"></i></span>
<span class="message">Security alert</span>
</a>
</li>
<li class="divider"></li>
<li class="message-preview">
<a href="#">
<span class="avatar"><i class="fa fa-bell"></i></span>
<span class="message">Security alert</span>
</a>
</li>
<li class="divider"></li>
<li><a href="#">Go to Inbox <span class="badge">2</span></a></li>
</ul>
</li>
<li class="dropdown user-dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> {{name}}<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="{{path('logout')}}"><i class="fa fa-power-off"></i> Log Out</a></li>
</ul>
</li>
</div>
</nav>
{% block body %}{% endblock %}
<!-- /#wrapper -->
<script type="text/javascript">
jQuery(function ($) {
var performance = [12, 43, 34, 22, 12, 33, 4, 17, 22, 34, 54, 67],
visits = [123, 323, 143, 132, 274, 223, 143, 156, 223, 223],
budget = [23, 19, 11, 34, 42, 52, 35, 22, 37, 45, 55, 57],
sales = [11, 9, 31, 34, 42, 52, 35, 22, 37, 45, 55, 57],
targets = [17, 19, 5, 4, 62, 62, 75, 12, 47, 55, 65, 67],
avrg = [117, 119, 95, 114, 162, 162, 175, 112, 147, 155, 265, 167];
$("#shieldui-chart1").shieldChart({
primaryHeader: {
text: "Visitors"
},
exportOptions: {
image: false,
print: false
},
dataSeries: [{
seriesType: "area",
collectionAlias: "Q Data",
data: performance
}]
});
$("#shieldui-chart2").shieldChart({
primaryHeader: {
text: "Login Data"
},
exportOptions: {
image: false,
print: false
},
dataSeries: [
{
seriesType: "polarbar",
collectionAlias: "Logins",
data: visits
},
{
seriesType: "polarbar",
collectionAlias: "Avg Visit Duration",
data: avrg
}
]
});
$("#shieldui-chart3").shieldChart({
primaryHeader: {
text: "Sales Data"
},
dataSeries: [
{
seriesType: "bar",
collectionAlias: "Budget",
data: budget
},
{
seriesType: "bar",
collectionAlias: "Sales",
data: sales
},
{
seriesType: "spline",
collectionAlias: "Targets",
data: targets
}
]
});
$("#shieldui-grid1").shieldGrid({
dataSource: {
data: gridData
},
sorting: {
multiple: true
},
paging: {
pageSize: 7,
pageLinksCount: 4
},
selection: {
type: "row",
multiple: true,
toggle: false
},
columns: [
{ field: "id", width: "70px", title: "ID" },
{ field: "name", title: "Person Name" },
{ field: "company", title: "Company Name" },
{ field: "email", title: "Email Address", width: "270px" }
]
});
});
</script>
{% endblock %}
</body>
</html>
**index.html.twig**
This is my **index.html.twig** in this file i have included my 'base.html.twig' in this file.
{% extends "::base.html.twig" %}
{% block body %}
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1>Dashboard <small>Dashboard Home</small></h1>
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Welcome to the admin dashboard!
</div>
</div>
</div>
<div class="row">
<div class="col-lg-3">
<div class="panel panel-default ">
<div class="panel-body alert-info">
<div class="col-xs-5">
<i class="fa fa-truck fa-5x"></i>
</div>
<div class="col-xs-5 text-right">
<p class="alerts-heading">0</p>
<p class="alerts-text">New Orders</p>
</div>
</div>
</div>
</div>
<div class="col-lg-3">
<div class="panel panel-default ">
<div class="panel-body alert-info">
<div class="col-xs-5">
<i class="fa fa-money fa-5x"></i>
</div>
<div class="col-xs-5 text-right">
<p class="alerts-heading">0</p>
<p class="alerts-text">Income</p>
</div>
</div>
</div>
</div>
<div class="col-lg-3">
<div class="panel panel-default ">
<div class="panel-body alert-info">
<div class="col-xs-5">
<i class="fa fa-twitter fa-5x"></i>
</div>
<div class="col-xs-5 text-right">
<p class="alerts-heading">0</p>
<p class="alerts-text">Mentions</p>
</div>
</div>
</div>
</div>
<div class="col-lg-3">
<div class="panel panel-default ">
<div class="panel-body alert-info">
<div class="col-xs-5">
<i class="fa fa-download fa-5x"></i>
</div>
<div class="col-xs-5 text-right">
<p class="alerts-heading">0</p>
<p class="alerts-text">Downloads</p>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> Sales personnel Data</h3>
</div>
<div class="panel-body">
<div id="shieldui-grid1"></div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> Logins per week</h3>
</div>
<div class="panel-body">
<div id="shieldui-chart2"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> Sales Data</h3>
</div>
<div class="panel-body">
<div id="shieldui-chart3"></div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
**users.php**
This is my **users.php** file i have used this file as a database.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\usersRepository")
*/
class users
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="user_name", type="string", length=255)
*/
private $userName;
/**
* @var string
*
* @ORM\Column(name="first_name", type="string", length=255)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set userName
*
* @param string $userName
*
* @return users
*/
public function setUserName($userName)
{
$this->userName = $userName;
return $this;
}
/**
* Get userName
*
* @return string
*/
public function getUserName()
{
return $this->userName;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return users
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set password
*
* @param string $password
*
* @return users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
}
答案 0 :(得分:0)
// src/AppBundle/Controller/DefaultController.php
class DefaultController extends Controller
{
public function yourAction()
{
$name = 'somethinghere';
return $this->render('yourtwigfile.html.twig', array(
'name' => $name,
));
}
// ...
}
并在你的twig文件中你的twig filename.html.twig ...
Your name here {{ name }}