我是Laravel 5的新手。
我有一个路线/subscribe
。
//Subcribe
Route::post('/subscribe','SubscribeController@postSubscribe');
当我转到它时,我的Laravel应用程序会自动将我重定向到:/auth/login
我注意到,在我的
中我有:/app/Http/routes.php
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
}
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller {
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Create a new password controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\PasswordBroker $passwords
* @return void
*/
public function __construct(Guard $auth, PasswordBroker $passwords)
{
$this->auth = $auth;
$this->passwords = $passwords;
$this->middleware('guest');
}
}
@SW请求
<?php
namespace App\Http\Controllers;
use App\Subscribe;
use Input, Validator, Auth, Redirect, Request, Session, Mail, View;
class SubscribeController extends Controller {
public function index()
{
$subscribes = Subscribe::all();
return View::make('subscribes.index')
->with('subscribes',$subscribes);
}
//------------------------------------------------------------------------------------------------- [ Create]
public function create()
{
return View::make('subscribes.create');
}
//------------------------------------------------------------------------------------------------- [ Store ]
public function store()
{
$validator = Subscribe::validator(Input::all());
if ($validator->fails()) {
return Redirect::to('subscribe/create')
->withErrors($validator)->withInput();
} else {
$subscribe = new Subscribe;
$subscribe->email = Input::get('email');
$subscribe->save();
return Redirect::to('/subscribe')
->with('success','The web directory was created succesfully!');
}
}
//------------------------------------------------------------------------------------------------- [ Show ]
public function show($id)
{
$subscribe = Subscribe::findOrFail($id);
return View::make('subscribes.show')
->with('subscribe', $subscribe);
}
//------------------------------------------------------------------------------------------------- [ Edit ]
public function edit($id)
{
$subscribe = Subscribe::findOrFail($id);
return View::make('subscribes.edit')
->with('subscribe', $subscribe );
}
//------------------------------------------------------------------------------------------------- [ Update ]
public function update($id)
{
$validation = Subscribe::validator(Input::all());
if ($validation->fails()) {
return Redirect::to('subscribe/'. $id . '/edit')->withErrors($validation);
} else {
$subscribe = Subscribe::findOrFail($id);
$subscribe->email = Input::get('email');
$subscribe->save();
return Redirect::to('subscribe')
->with('success','The web directory was updated succesfully!');
}
}
//------------------------------------------------------------------------------------------------- [ Destroy ]
public function destroy($id){
$subscribe = Subscribe::find($id);
$subscribe->delete();
return Redirect::to('subscribe')
->with('success','The web directory was deleted succesfully!');
}
public function postSubscribe() {
$subscribe_email = Input::only('subscribe_email');
// Validation
$validator = Validator::make( $subscribe_email ,
array(
'subscribe_email' => 'email|unique:subscribes,email',
)
);
if ($validator->fails()) {
return Redirect::to('/#footer')
->with('subscribe_error', $subscribe_email['subscribe_email']. ' is already an Aveniros subscriber.')
->withErrors($validator)->withInput();
}else{
$subscribe = new Subscribe;
$subscribe->email = Input::get('subscribe_email');
$subscribe->save();
return Redirect::to('/thank-you');
}
}
public function postSubscribeAjax() {
$data = Request::all();
//dd($data); Stuck HERE
// Validation
$validator = Validator::make( $data,
array(
'subscribe_email' => 'email|unique:subscribes,email',
)
);
if ($validator->fails()) {
return Redirect::to('/#footer')
->with('subscribe_error','This email is already subscribed to us.')
->withErrors($validator)->withInput();
}else{
$subscribe = new Subscribe;
$subscribe->email = Input::get('subscribe_email');
$subscribe->save();
return Redirect::to('/thank-you');
}
}
public function thankyou() {
return view('subscribes.thankyou');
}
}
为什么?以及如何解决?这甚至是正常行为吗?
答案 0 :(得分:1)
你需要为你的每条路线设置一个方法,告诉Laravel如何对待每条路线。例如,在路径文件中,应该有Route::get('index@SubscribeController);
行。正如Staurt Wagner所提到的,如果你只是导航到一个路径,你就是向服务器发送get请求。
如果您使用的是Laravel 5,您的控制器应位于\app\Http\Controllers
路径中。
index
中的SubscribeController
方法可以简单地为:
public function index(){
return "Hello World!";
}
有关详尽的教程,请参阅Laracast上的this wonderful series。