This commit is contained in:
Tomas Husak 2021-03-09 18:06:53 +01:00
Родитель 19832ca5a2
Коммит 4646765e71
17 изменённых файлов: 83 добавлений и 182 удалений

Просмотреть файл

@ -11,7 +11,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\BlazorApp.Shared.csproj" />
<ProjectReference Include="..\..\..\..\Source\PhpBlazor\PhpBlazor\PhpBlazor.csproj" />
<ProjectReference Include="..\..\..\Demo-Web\Web\Web.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -1,46 +0,0 @@
@page "/fetchdata"
@using BlazorApp.Shared
@inject HttpClient Http
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[] forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
}

Просмотреть файл

@ -0,0 +1,20 @@
@page "/community/{*sth}"
@page "/about/{*sth}"
@page "/index"
@page "/simpleComponent"
@using PhpBlazor
<PhpScriptProvider ContextLifetime="@SessionLifetime.OnNavigationChanged" Type="@PhpScriptProviderType.ScriptProvider">
<Navigating>
<PhpScriptProvider ContextLifetime="@SessionLifetime.OnNavigationChanged" Type="@PhpScriptProviderType.Script" ScriptName="navigating.php" />
</Navigating>
<NotFound>
<PhpScriptProvider ContextLifetime="@SessionLifetime.OnNavigationChanged" Type="@PhpScriptProviderType.Script" ScriptName="notfound.php" />
</NotFound>
</PhpScriptProvider>
@code
{
[Parameter]
public string sth { get; set; }
}

Просмотреть файл

@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PhpBlazor;
using System;
using System.Collections.Generic;
using System.Net.Http;
@ -17,7 +18,8 @@ namespace BlazorApp.Client
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// Add PHP
builder.AddPhp(new[] { typeof(force).Assembly });
await builder.Build().RunAsync();
}

Просмотреть файл

@ -18,8 +18,8 @@
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="fetchdata">
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
<NavLink class="nav-link" href="index">
<span class="oi oi-list-rich" aria-hidden="true"></span> Php Web
</NavLink>
</li>
</ul>

Просмотреть файл

@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
@ -9,6 +9,9 @@
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="BlazorApp.Client.styles.css" rel="stylesheet" />
<link href="Asteroids/css/styles.css" rel="stylesheet" />
<script src="js/php.js"></script>
</head>
<body>

Просмотреть файл

@ -9,8 +9,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Source\PhpBlazor\PhpBlazor\PhpBlazor.csproj" />
<ProjectReference Include="..\Client\BlazorApp.Client.csproj" />
<ProjectReference Include="..\Shared\BlazorApp.Shared.csproj" />
</ItemGroup>

Просмотреть файл

@ -1,40 +0,0 @@
using BlazorApp.Shared;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorApp.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

Просмотреть файл

@ -1,42 +0,0 @@
@page
@model BlazorApp.Server.Pages.ErrorModel
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Error</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="~/css/app.css" rel="stylesheet" />
</head>
<body>
<div class="main">
<div class="content px-4">
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}
<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
</div>
</div>
</body>
</html>

Просмотреть файл

@ -1,32 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorApp.Server.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
private readonly ILogger<ErrorModel> _logger;
public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}
public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
}

Просмотреть файл

@ -4,7 +4,10 @@ using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;
using System.Linq;
namespace BlazorApp.Server
@ -20,12 +23,7 @@ namespace BlazorApp.Server
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
}
public void ConfigureServices(IServiceCollection services){}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
@ -46,12 +44,26 @@ namespace BlazorApp.Server
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
// Add helper js code for php.
var fileProvider = new ManifestEmbeddedFileProvider(typeof(PhpBlazor.BlazorContext).Assembly);
app.UseStaticFiles(new StaticFileOptions() { FileProvider = fileProvider });
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName, "Demo-Web\\Web\\wwwroot")),
RequestPath = "/Web"
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName, "Demo-PhpComponent\\Asteroids\\wwwroot")),
RequestPath = "/Asteroids"
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}

Просмотреть файл

@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31019.35
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Server", "BlazorApp\Server\BlazorApp.Server.csproj", "{47143225-0F99-4EAF-9470-DB0726F757F8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Server", "BlazorApp\Server\BlazorApp.Server.csproj", "{47143225-0F99-4EAF-9470-DB0726F757F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Client", "BlazorApp\Client\BlazorApp.Client.csproj", "{05B77E14-56F3-47BD-B630-7695136D46F4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Client", "BlazorApp\Client\BlazorApp.Client.csproj", "{05B77E14-56F3-47BD-B630-7695136D46F4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Shared", "BlazorApp\Shared\BlazorApp.Shared.csproj", "{A35812D0-7A77-48D4-B0C7-58D812A72575}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web", "..\Demo-Web\Web\Web.csproj", "{2C056495-A221-43BE-BAB1-1FECA35F2A1D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PhpBlazor", "..\..\Source\PhpBlazor\PhpBlazor\PhpBlazor.csproj", "{1DA8C8AF-37B8-40DB-9508-70A51386610D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -23,10 +25,14 @@ Global
{05B77E14-56F3-47BD-B630-7695136D46F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05B77E14-56F3-47BD-B630-7695136D46F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05B77E14-56F3-47BD-B630-7695136D46F4}.Release|Any CPU.Build.0 = Release|Any CPU
{A35812D0-7A77-48D4-B0C7-58D812A72575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A35812D0-7A77-48D4-B0C7-58D812A72575}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A35812D0-7A77-48D4-B0C7-58D812A72575}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A35812D0-7A77-48D4-B0C7-58D812A72575}.Release|Any CPU.Build.0 = Release|Any CPU
{2C056495-A221-43BE-BAB1-1FECA35F2A1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C056495-A221-43BE-BAB1-1FECA35F2A1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C056495-A221-43BE-BAB1-1FECA35F2A1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C056495-A221-43BE-BAB1-1FECA35F2A1D}.Release|Any CPU.Build.0 = Release|Any CPU
{1DA8C8AF-37B8-40DB-9508-70A51386610D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DA8C8AF-37B8-40DB-9508-70A51386610D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DA8C8AF-37B8-40DB-9508-70A51386610D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DA8C8AF-37B8-40DB-9508-70A51386610D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Просмотреть файл

@ -0,0 +1,9 @@
<?php namespace something;
#[\Microsoft\AspNetCore\Components\RouteAttribute("simpleComponent")]
class ExampleComponent extends \PhpBlazor\PhpComponent
{
public function BuildRenderTree($builder) : void
{
$builder->AddMarkupContent(0, "<h1>Simple php component</h1>");
}
}

Просмотреть файл

@ -26,6 +26,8 @@ public static async Task Main(string[] args)
<p>You can try the offline mode by disabling it in the developer tools.</p>
<p>You can navigate through the website as normal, but external resources (like images) can not be loaded.</p>
<img src="Web/images/offlineMode.png"/>
<h3>Compatible with PhpComponents</h3>
Try to navigate to the <a href="/simpleComponent">component</a>
<?php
require("/footers/defaultFooter.php");
?>

Просмотреть файл

@ -0,0 +1 @@
<p>Navigating to script...</p>

Просмотреть файл

@ -0,0 +1 @@
<p>Page not found</p>

Просмотреть файл

@ -53,6 +53,7 @@ namespace PhpBlazor
private bool _navigationInterceptionEnabled;
private BlazorContext _ctx;
private string _previousRelativeUri = null;
private bool _navigated = false;
#region IComponent
void IComponent.Attach(RenderHandle renderHandle)
@ -74,6 +75,7 @@ namespace PhpBlazor
if (ContextLifetime == SessionLifetime.OnNavigationChanged || _ctx == null)
_ctx = BlazorContext.Create(this);
_navigated = true;
Refresh();
return Task.CompletedTask;
}
@ -81,6 +83,7 @@ namespace PhpBlazor
private void Refresh()
{
_navigated = false;
// Find Script
var relativeUri = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
var querryParameters = QueryHelpers.ParseQuery(NavigationManager.ToAbsoluteUri(relativeUri).Query);
@ -149,7 +152,8 @@ namespace PhpBlazor
if (relativeUri.IndexOf('?') > -1)
relativeUri = relativeUri.Substring(0, relativeUri.IndexOf('?'));
if (Type == PhpScriptProviderType.ScriptProvider || (Type == PhpScriptProviderType.Script && relativeUri != _previousRelativeUri))
if ((Type == PhpScriptProviderType.Script && relativeUri != _previousRelativeUri)
|| (Type == PhpScriptProviderType.ScriptProvider && _navigated))
{
_previousRelativeUri = relativeUri;
return;