我在classes
文件夹中创建了一个app
文件夹,并在Alert.php
内创建了app\classes
。
在app\start\global.php
中,代码是这样的:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/classes', // This line is the one I've added.
));
在composer.joson
:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/classes"
]
在此之后我跑了composer dump-autoload
我在'Alert'=>'Alert'
中的aliases
数组中添加了app\config\app.php
我的班级app\classes\Alert.php
:
<?php
class Alert {
static function new($type, $message){
if(Session::has("alert_type") && Session::has("alert_message")) {
$alert_type = Session::get("alert_type");
$alert_message = Session::get("alert_message");
} else {
$alert_type = array();
$alert_message = array();
}
$alert_type[] = $type;
$alert_message[] = $message;
Session::flash("alert_type", $alert_type);
Session::flash("alert_message", $alert_message);
}
}
在filters.php
:
Route::filter('auth', function()
{
if (Auth::guest()) {
$alert_type = "danger";
$alert_message = "<strong>Error: </strong>You need to sign in before continue.";
Alert::new($alert_type, $alert_message);
return Redirect::intended('/');
}
});