Laravel: 将数据从数据库传递到路由
Laravel: Passing data into route from database
我已经创建了一个主题系统,我正在尝试让主题的路由文件从数据库中拉取页面并为其分配一个路由,下面是我的主题路由文件。我遇到的问题是来自我的 ThemeSettings::all
模型请求的 Undefined variable: theme
。
Route.php(在主题中)
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use App\Models\ThemeSettings;
use App\Models\Pages;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('themes/main/index');
})->name('home');
$theme = ThemeSettings::all();
$pages = Pages::get();
foreach ($pages as $key => $page) {
Route::get('/' . $page->slug, function () {
return view($theme->location . $theme->name . '/' . $page->view);
})->name($page->slug);
}
Route::get('/terms', function () {
return 'Terms of use';
})->name('termsofuse');
Route::get('/privacy', function () {
return 'Privacy Policy';
})->name('privacypolicy');
Route::get('posts', 'PostController@index')->name('post.index');
Route::get('post/{slug}', 'PostController@details')->name('post.details');
Route::get('/category/{slug}', 'PostController@postByCategory')->name('category.posts');
Route::get('/tag/{slug}', 'PostController@postByTag')->name('tag.posts');
Route::get('/search', 'SearchController@search')->name('search');
Route::group(['middleware' => ['auth']], function () {
Route::post('favorite/{post}/add', 'FavoriteController@add')->name('post.favorite');
Route::post('comment/{post}', 'CommentController@store')->name('comment.store');
});
ThemeSettings 迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateThemeSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('theme_settings', function (Blueprint $table) {
$table->id();
$table->string('name')->default('main');
$table->text('description');
$table->string('author');
$table->string('location')->default('resources/views/themes');
$table->string('view_location')->default('themes/');
$table->timestamps();
$table->boolean('is_theme')->default(false);
});
DB::table('theme_settings')->insert([
'description' => 'The core theme for Oracle CMS. A custom responsive framework with easy to use controls.',
'location' => 'resources/views/themes/',
'view_location' => 'themes/',
'author' => 'Spencer K Media'
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('theme_settings');
}
}
这些设置在网站的其他部分(包括管理区域)都有效,这就是我感到困惑的原因。
如有任何帮助,我们将不胜感激!提前致谢!
$theme
和 $page
不在闭包范围内:
Route::get('/' . $page->slug, function () {
// $theme and $page are not defined here
return view($theme->location . $theme->name . '/' . $page->view);
})->name($page->slug);
因此,您可以通过 use()
访问它们
Route::get('/' . $page->slug, function () use ($theme, $page) {
// All good now
return view($theme->location . $theme->name . '/' . $page->view);
})->name($page->slug);
但是,你还有一个问题:
// $theme is a collection instance
$theme = ThemeSettings::all();
意味着像 $theme->location
一样访问 属性 也会导致异常。
// You need to limit the result to a single instance.
$theme = ThemeSettings::where('something', 'some_value')->first();
我已经创建了一个主题系统,我正在尝试让主题的路由文件从数据库中拉取页面并为其分配一个路由,下面是我的主题路由文件。我遇到的问题是来自我的 ThemeSettings::all
模型请求的 Undefined variable: theme
。
Route.php(在主题中)
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use App\Models\ThemeSettings;
use App\Models\Pages;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('themes/main/index');
})->name('home');
$theme = ThemeSettings::all();
$pages = Pages::get();
foreach ($pages as $key => $page) {
Route::get('/' . $page->slug, function () {
return view($theme->location . $theme->name . '/' . $page->view);
})->name($page->slug);
}
Route::get('/terms', function () {
return 'Terms of use';
})->name('termsofuse');
Route::get('/privacy', function () {
return 'Privacy Policy';
})->name('privacypolicy');
Route::get('posts', 'PostController@index')->name('post.index');
Route::get('post/{slug}', 'PostController@details')->name('post.details');
Route::get('/category/{slug}', 'PostController@postByCategory')->name('category.posts');
Route::get('/tag/{slug}', 'PostController@postByTag')->name('tag.posts');
Route::get('/search', 'SearchController@search')->name('search');
Route::group(['middleware' => ['auth']], function () {
Route::post('favorite/{post}/add', 'FavoriteController@add')->name('post.favorite');
Route::post('comment/{post}', 'CommentController@store')->name('comment.store');
});
ThemeSettings 迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateThemeSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('theme_settings', function (Blueprint $table) {
$table->id();
$table->string('name')->default('main');
$table->text('description');
$table->string('author');
$table->string('location')->default('resources/views/themes');
$table->string('view_location')->default('themes/');
$table->timestamps();
$table->boolean('is_theme')->default(false);
});
DB::table('theme_settings')->insert([
'description' => 'The core theme for Oracle CMS. A custom responsive framework with easy to use controls.',
'location' => 'resources/views/themes/',
'view_location' => 'themes/',
'author' => 'Spencer K Media'
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('theme_settings');
}
}
这些设置在网站的其他部分(包括管理区域)都有效,这就是我感到困惑的原因。
如有任何帮助,我们将不胜感激!提前致谢!
$theme
和 $page
不在闭包范围内:
Route::get('/' . $page->slug, function () {
// $theme and $page are not defined here
return view($theme->location . $theme->name . '/' . $page->view);
})->name($page->slug);
因此,您可以通过 use()
Route::get('/' . $page->slug, function () use ($theme, $page) {
// All good now
return view($theme->location . $theme->name . '/' . $page->view);
})->name($page->slug);
但是,你还有一个问题:
// $theme is a collection instance
$theme = ThemeSettings::all();
意味着像 $theme->location
一样访问 属性 也会导致异常。
// You need to limit the result to a single instance.
$theme = ThemeSettings::where('something', 'some_value')->first();