Advertisement
Guest User

Untitled

a guest
Jan 29th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. Добавляем dictator (Мой генератор админки) в зависимости:
  2. composer config minimum-stability dev
  3. composer require "uavn/dictator"
  4.  
  5.  
  6. В конфиги config/dev.php и config/prod.php добавь параметры подключения к БД:
  7. $app['db.options'] = [
  8. 'host' => 'localhost',
  9. 'user' => 'root',
  10. 'password' => 'root',
  11. 'dbname' => 'vladia'
  12. ];
  13.  
  14. Только настрой согласно своим параметрам БД
  15.  
  16. кроме того в config/prod.php добавь:
  17. // Admin: count items on list page
  18. $app['admin.items_per_page'] = 10;
  19.  
  20.  
  21. в src/app.php перед return $app; вставь:
  22. // PDO Service
  23. $app['db'] = function() use ($app) {
  24. return new \PDO(
  25. sprintf(
  26. 'mysql:host=%s;dbname=%s',
  27. $app['db.options']['host'],
  28. $app['db.options']['dbname']
  29. ),
  30. $app['db.options']['user'],
  31. $app['db.options']['password'],
  32. [
  33. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  34. ]
  35. );
  36. };
  37.  
  38. // Admin Generator Service
  39. $app['dictator'] = function() use ($app) {
  40. return (new \Uavn\Dictator)
  41. ->setConnection($app['db'])
  42. ->setOptions([
  43. 'Actions' => 'Действия',
  44. 'Edit' => 'Редактировать',
  45. 'Delete' => 'Удалить',
  46. 'New row' => 'Новая запись',
  47. 'Edit row' => 'Редактировать запись',
  48. 'Page' => 'Страница',
  49. 'Sure?' => 'Уверены?',
  50. 'Search' => 'Найти',
  51. 'Save' => 'Сохранить',
  52. 'List rows' => 'Все записи',
  53. 'Saved successful' => 'Успешно сохранено',
  54. 'ipp' => $app['admin.items_per_page']
  55. ]);
  56. };
  57.  
  58.  
  59. далее админка, добавь в файл config/prod.php
  60.  
  61. $app['admin.credentials'] = [
  62. 'admin' => 'admin'
  63. ];
  64.  
  65. по этим логин-пароль можно будет попасть в админку
  66.  
  67.  
  68.  
  69. создай файл src/controllers/admin.php папку src/controllers предварительно тоже создай
  70. в файл src/controllers/admin.php запиши следующее:
  71. <?php
  72.  
  73. use Symfony\Component\HttpFoundation\Request;
  74. use Symfony\Component\HttpFoundation\Response;
  75. use Symfony\Component\HttpFoundation\JsonResponse;
  76. use Symfony\Component\HttpFoundation\RedirectResponse;
  77. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  78. use Silex\Application;
  79.  
  80. // define controllers for a blog
  81. $admin = $app['controllers_factory'];
  82.  
  83. // HTTP Auth
  84. $admin->before(function(Request $request, Application $app) {
  85. $users = $app['admin.credentials'];
  86.  
  87. $user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
  88. $pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
  89.  
  90. $validated = (array_key_exists($user, $users)) && ($pass == $users[$user]);
  91.  
  92. if (!$validated) {
  93. header('WWW-Authenticate: Basic realm="Security Zone"');
  94. header('HTTP/1.0 401 Unauthorized');
  95. die ("Not authorized");
  96. }
  97. });
  98.  
  99. $admin->get('/', function (Application $app) {
  100.  
  101. return $app['twig']->render('admin/index.html.twig', [
  102. ]);
  103. })->bind('admin.index');
  104.  
  105. // Prefix /admin for all actions
  106. $app->mount('/admin', $admin);
  107.  
  108. в файл src/controllers.php допиши после всех use-ов:
  109. require_once __DIR__ . '/controllers/admin.php';
  110.  
  111. создай файл templates/admin-layout.html.twig:
  112. <!DOCTYPE html>
  113. <html lang="en">
  114. <head>
  115. <meta charset="utf-8">
  116. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  117. <meta name="viewport" content="width=device-width, initial-scale=1">
  118. <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  119.  
  120. <title>{% block title 'Vladia' %}</title>
  121.  
  122. <!-- Latest compiled and minified CSS -->
  123. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  124.  
  125. <link href="{{ asset('css/adminstyle.css') }}" rel="stylesheet">
  126. <link href="{{ asset('css/main.css') }}" rel="stylesheet" type="text/css" />
  127.  
  128. <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  129. <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  130. <!--[if lt IE 9]>
  131. <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  132. <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  133. <![endif]-->
  134. </head>
  135. <body>
  136. <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  137. <div class="container-fluid">
  138. <div class="collapse navbar-collapse">
  139. <ul class="nav navbar-nav">
  140. <li>
  141. <a href="{{ url('admin.index') }}">
  142. <span class="glyphicon glyphicon-list-alt"></span>
  143. Админка
  144. </a>
  145. </li>
  146.  
  147. <li>
  148. <a href="{{ url('admin.category') }}">
  149. <span class="glyphicon glyphicon-list-alt"></span>
  150. Категории
  151. </a>
  152. </li>
  153.  
  154. <li>
  155. <a href="{{ url('admin.section') }}">
  156. <span class="glyphicon glyphicon-list-alt"></span>
  157. Секции
  158. </a>
  159. </li>
  160.  
  161. <li>
  162. <a href="{{ url('admin.product') }}">
  163. <span class="glyphicon glyphicon-list-alt"></span>
  164. Товары
  165. </a>
  166. </li>
  167. </ul>
  168. </div><!-- /.nav-collapse -->
  169. </div><!-- /.container -->
  170. </div><!-- /.navbar -->
  171.  
  172.  
  173. <div class="container-fluid">
  174. <div class="_row">
  175. {% block content %}{% endblock%}
  176. </div>
  177. </div>
  178.  
  179. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  180. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  181.  
  182. <script>
  183. $(document).on('click', 'th input[type=checkbox]', function() {
  184. if ( $(this).is(':checked') ) {
  185. $('td input[type=checkbox]').prop('checked', true);
  186. } else {
  187. $('td input[type=checkbox]').prop('checked', false);
  188. }
  189. });
  190. $(document).on('click', 'td input[type=checkbox]', function() {
  191. if( $('td input[type=checkbox]:checked').length == $('td input[type=checkbox]').length ) {
  192. $('th input[type=checkbox]').prop('checked', true);
  193. } else {
  194. $('th input[type=checkbox]').prop('checked', false);
  195. }
  196. });
  197. </script>
  198.  
  199. <!-- Latest compiled and minified JavaScript -->
  200. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  201. </body>
  202. </html>
  203.  
  204.  
  205. и хак для интеграции админки в бутстрап, создай файл web/css/adminstyle.css:
  206. body {
  207. padding-top: 50px;
  208. }
  209. h1 .new {
  210. font-size: 14px;
  211. }
  212.  
  213. .dictator-search {
  214. background-color: #eee;
  215. padding: 10px;
  216. margin: 20px 0;
  217. }
  218.  
  219. .dictator-search label {
  220. margin-right: 10px;
  221. }
  222.  
  223. .dictator-sort-asc, .dictator-sort-desc {
  224. padding-left: 10px;
  225. background-repeat: no-repeat;
  226. background-position: -6px -3px;
  227. }
  228. .dictator-sort-desc {
  229. background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABZ0RVh0Q3JlYXRpb24gVGltZQAwNC8xNC8wOFZL9aYAAAAcdEVYdFNvZnR3YXJlAEFkb2JlIEZpcmV3b3JrcyBDUzQGstOgAAAAj0lEQVQ4jWP8//8/AzUBE1VNGzVwhBjIgk9yr4AAzkTq/OEDI8kGMjAwMJhWVGCIne7owKker5ddPn5khGn+sX8/3LBc459YXcfAwMDA8P//f5wYJr+Hn///x/b2/3v4+f/jU////38GRnx5mZER4ZA9/Pz/t0+RZfr+8zMTAwPD36nJD7DrGS0cRg0kHQAAYMtZjBTUt+kAAAAASUVORK5CYII=');
  230. }
  231. .dictator-sort-asc {
  232. background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABZ0RVh0Q3JlYXRpb24gVGltZQAwNC8xNC8wOFZL9aYAAAAcdEVYdFNvZnR3YXJlAEFkb2JlIEZpcmV3b3JrcyBDUzQGstOgAAAApUlEQVQ4je2TMQ7CMAxFv6MKIYbEV2HIASquwcYhmhv0BD0NibpxDkbGsrGkZkB0ajxAByT6JUuWv/6TLNkkIlhSZlHaCvwTYKWZRDT10Tm57XfV5bgBgNydrvMhESnW24/Oyb1tJTJLtM5oGXXls7WUmMWHgEffwzcNYCgn5mJOBRLR6EMAAGzrGgBeUCAXM9ovJ+aieRgGmpurwE/0+3e4Ar/XEybDVp50x3YfAAAAAElFTkSuQmCC');
  233. }
  234.  
  235. .dictator-pager {
  236. margin: 30px 0;
  237. }
  238. .dictator-current {
  239. background-color: #fff000;
  240. }
  241. .dictator-page, .dictator-current {
  242. padding: 3px;
  243. }
  244. .dictator-pager a {
  245. color: #000;
  246. }
  247.  
  248. th .form-control[type=checkbox], td .form-control[type=checkbox] {
  249. height: 15px;
  250. width: 15px;
  251. }
  252.  
  253. .table {
  254. margin-bottom: 0 !important;
  255. }
  256.  
  257. .nowrap {
  258. white-space: nowrap;
  259. }
  260.  
  261. .nowrap a {
  262. color: #000;
  263. }
  264. .nowrap a:hover {
  265. text-decoration: none;
  266. border-bottom: 1px solid #2A6496;
  267. color: #2A6496;
  268. }
  269.  
  270. .dictator-row {
  271. max-width: 800px;
  272. margin-bottom: 20px;
  273. }
  274.  
  275. .dictator-row [type=checkbox] {
  276. width: 15px;
  277. height: 15px;
  278. display: inline;
  279. vertical-align: middle;
  280. margin: 0;
  281. }
  282.  
  283. .dictator-row textarea {
  284. min-height: 300px;
  285. }
  286.  
  287. .dictator-list-rows {
  288. margin-left: 20px;
  289. }
  290.  
  291. .dictator-saved {
  292. background-color: #DFF0D8;
  293. border-color: #D6E9C6;
  294. color: #3C763D;
  295.  
  296. padding: 15px;
  297. margin-bottom: 20px;
  298. border: 1px solid rgba(0, 0, 0, 0);
  299. border-radius: 4px;
  300. }
  301.  
  302. .wysibb .powered {
  303. display: none;
  304. }
  305.  
  306. th {
  307. background-color: #333 !important;
  308. color: #fff;
  309. border-color: #111 !important;
  310. border-top: 1px solid #111 !important;
  311. }
  312. th a {
  313. color: #fff;
  314. }
  315.  
  316. .inp-little {
  317. display: inline !important;
  318. max-width: 200px;
  319. }
  320.  
  321. .info-row {
  322. margin-bottom: 10px;
  323. }
  324.  
  325. .shop-item {
  326. float: left;
  327. margin-right: 10px;
  328. }
  329.  
  330. .clr {
  331. clear: both;
  332. }
  333.  
  334. .one-less-shop {
  335. float: left;
  336. margin-top: 25px;
  337. }
  338.  
  339. .shop-row {
  340. margin-bottom: 20px;
  341. }
  342.  
  343. .object-tobuy {
  344. margin-bottom: 10px;
  345. }
  346.  
  347. body {
  348. }
  349.  
  350.  
  351. #msg {
  352. width: 900px;
  353. height: 300px;
  354. }
  355.  
  356. .dictator-search label {
  357. vertical-align: top;
  358. }
  359. .dictator-search .selectize-control {
  360. width: 200px;
  361. }
  362.  
  363. .dictator-search .btn {
  364. margin-top: 20px;
  365. }
  366.  
  367. #files img {
  368. width: 150px;
  369. display: inline-block;
  370. margin: 5px;
  371. }
  372.  
  373. #files img:hover {
  374. opacity: 0.8;
  375. cursor: pointer;
  376. }
  377.  
  378. создай файл templates/admin/index.html.twig:
  379.  
  380. {% extends "admin-layout.html.twig" %}
  381.  
  382. {% block content %}
  383. <div class="row">
  384. <div class="content">
  385. Welcome to your Admin Page
  386. </div>
  387. </div>
  388. {% endblock %}
  389.  
  390.  
  391. создай файл templates/admin/dictator.html.twig:
  392. {% extends "admin-layout.html.twig" %}
  393.  
  394. {% block content %}
  395. <div class="row">
  396. <div class="col-md-12">
  397. {{ html|raw }}
  398. </div>
  399. </div>
  400. {% endblock %}
  401.  
  402.  
  403.  
  404. Далее исправь titile в таблице product на title
  405.  
  406.  
  407. Далее добавь перед $app->mount... в controllers/admin.php:
  408. $admin->match('/category', function (Application $app) {
  409. $dictator = $app['dictator']
  410. ->setTable('category', 'Категории')
  411.  
  412. ->addField('id', 'ID')
  413.  
  414. ->addField('name', 'Название')
  415. ;
  416.  
  417. return $app['twig']->render('admin/dictator.html.twig', [
  418. 'html' => $dictator->generate()
  419. ]);
  420. })->bind('admin.category');
  421.  
  422. $admin->match('/section', function (Application $app) {
  423. $dictator = $app['dictator']
  424. ->setTable('section', 'Секции')
  425.  
  426. ->addField('id', 'ID')
  427.  
  428. ->addField('name', 'Название')
  429. ;
  430.  
  431. return $app['twig']->render('admin/dictator.html.twig', [
  432. 'html' => $dictator->generate()
  433. ]);
  434. })->bind('admin.section');
  435.  
  436. $admin->match('/product', function (Application $app) {
  437. $dictator = $app['dictator']
  438. ->setTable('product', 'Товары')
  439.  
  440. ->addField('id', 'ID')
  441.  
  442. ->addField('title', 'Название')
  443.  
  444. ->addField('text', 'Описание')
  445. ->addTextWidget('text')
  446. ->addFilter('text', function( $text ) {
  447. if ( $text ) {
  448. $substr = mb_substr($text, 0, 200, 'UTF-8');
  449. return $substr . '…';
  450. }
  451.  
  452. return '—';
  453. })
  454.  
  455. ->addField('price', 'Цена')
  456. ->addFilter('price', function( $price ) {
  457. if ( $price ) {
  458. $nf = number_format($price, 2, '.', ' ');
  459.  
  460. return $nf . ' грн';
  461. }
  462.  
  463. return '—';
  464. })
  465.  
  466. ->addField('sectionId', 'Секция')
  467. ->addRelation('sectionId', 'category', 'name')
  468.  
  469. ->addField('categoryId', 'Категория')
  470. ->addRelation('categoryId', 'category', 'name')
  471. ;
  472.  
  473. return $app['twig']->render('admin/dictator.html.twig', [
  474. 'html' => $dictator->generate()
  475. ]);
  476. })->bind('admin.product');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement