使得robots.txt由程式產生,加入若干html meta,加入自動產生html title的機制

This commit is contained in:
kroutony 2020-02-22 21:23:03 +08:00
parent 0bbd14de7d
commit 57fedd69d4
15 changed files with 117 additions and 6 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
/**
* 靜態頁面的controller
*
* Class PageController
* @package App\Http\Controllers
*/
class PageController extends Controller
{
public function index()
{
return view('index');
}
/**
* Robots.txt
*
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function robotstxt(Request $request)
{
if(app('Option')->block_search_indexing) {
$content = view('components.robotsTxt.disallowAll');
} else {
$content = view('components.robotsTxt.default');
}
return response($content)->header('Content-type', 'text/plain');
}
}

View File

@ -47,6 +47,10 @@ class SetSiteStates
//預設語系
$siteState->defaultLanguage = config('app.fallback_locale');
//設定預設的meta tag
$siteState->htmlMeta['description'] = app('Option')->site_description;
$siteState->htmlMeta['og:type'] = 'website';
return $next($request);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Presenters;
use Illuminate\Support\Facades\View;
class DocumentTitlePresenter
{
public function get()
{
$siteName = app('SiteName')->get();
$titleSection = trim(View::getSection('title'));
if(View::hasSection('title') && $titleSection) {
return $titleSection . ' - ' . $siteName;
} else {
return $siteName;
}
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Presenters;
class SiteNamePresenter
{
private $siteName;
public function __construct()
{
$this->siteName = $this->init();
}
public function init()
{
$configSiteName = config('app.name', 'Laravel');
try {
$optionSiteName = app('Option')->getOption('site_name');
if(!$optionSiteName) {
return $configSiteName;
}
} catch(\Exception $e) {
return $configSiteName;
}
return $optionSiteName;
}
public function get()
{
return $this->siteName;
}
}

View File

@ -42,7 +42,10 @@ class SingletonServiceProvider extends ServiceProvider
return new \App\Presenters\Html\HtmlPresenter;
});
$this->app->alias(\App\Presenters\SiteNamePresenter::class, 'SiteName');
$this->app->singleton(\App\Presenters\SiteNamePresenter::class, function($app){
return new \App\Presenters\SiteNamePresenter;
});
}
/**

View File

@ -3,7 +3,4 @@
@section('title', 'Admin Area')
@section('admin-page-content')
<pre>
{{ print_r(app('AdminMenu')->getMenu()) }}
</pre>
@endsection

View File

@ -1,6 +1,7 @@
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
@include('components.head.title')
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

@ -1,4 +1,9 @@
@extends('admin.layouts.app')
@section('title')
@lang('adminPageHeader.options.' . $slug)
@endsection
@section('admin-page-content')
<div class="row">
<div class="col">

View File

@ -0,0 +1,2 @@
<meta name="robots" content="noindex">
<meta name="googlebot" content="noindex">

View File

@ -0,0 +1,3 @@
@foreach(@app('SiteState')->htmlMeta as $name => $content)
<meta name="{{ $name }}" content="{{ $content }}">
@endforeach

View File

@ -0,0 +1,2 @@
@inject('documentTitlePresenter', App\Presenters\DocumentTitlePresenter)
<title>{{ $documentTitlePresenter->get() }}</title>

View File

@ -0,0 +1,2 @@
User-agent: *
Disallow: /{{ config('admin.route') }}/

View File

@ -1,2 +1,2 @@
User-agent: *
Disallow:
Disallow: /

View File

@ -1,11 +1,15 @@
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
@include('components.head.title')
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
@include('components.head.metaTags')
@if(app('Option')->block_search_indexing)
@include('components.head.metaNoindex')
@endif
@yield('head-meta')
@stack('app-head-scripts')

View File

@ -21,6 +21,8 @@ Auth::routes(['verify' => true]);
*/
Route::view('/', 'index')->name('index');
Route::get('/robots.txt', 'PageController@robotstxt');
/**
* Admin routes
*/