我正在使用Laravel开发一个用于移动应用程序的API,并且在嵌套路由上遇到了Route Model Binding的一些问题.该应用程序将有一个独立的sqlite数据库,当网络可用时,它将同步客户端旅程与中央服务器.因此,应用程序中的PK不能用于访问中央服务器上的记录.每个用户都有一个唯一的用户名,该用户名将存储在具有以下列的表中的中央服务器上:
- 用户身份
- 用户名
然后,旅程表将包含以下列:
- journey_id
- 用户身份
- user_journey_id
其中user_journey_id将是客户端设备上的旅程记录的PK.我们的想法是客户可以使用以下内容访问api:http:
//example.com/api/client/UNIQUE_USERNAME/journey/1234
以从中央服务器检索旅程.
我有以下资源设置:
Route::resource('client','ClientController');
Route::resource('client.journey','JourneyController');
Run Code Online (Sandbox Code Playgroud)
并为客户端成功设置路由模型绑定,如下所示:
$router->bind('client', function($value, $route) {
return \App\Client::where('username', '=', $value)->firstOrFail();
});
Run Code Online (Sandbox Code Playgroud)
我在设置嵌套模型绑定时遇到了一些麻烦,因为我需要将客户端username与其结合user_journey_id来检索正确的旅程.有没有办法用路由模型绑定来做到这一点?
或者这应该在控制器中完成,例如:
public function show(Client $client, $user_journey_id)
{
... // have logic here to get the journey.
Run Code Online (Sandbox Code Playgroud)
这就是我目前的做法,但路线模型绑定肯定会使它更容易一些.
我'将项目从localhost上传到我的专用服务器,经过这么多问题,最后一些页面的工作原理是domain.com domain.com/home | domain.com/allsites等..
但现在,路由"domain.com/site/create""domain.com/site/ID/manage","domain.com/site/ID/edit"未找到,我收到此错误,为什么?
FileViewFinder.php第137行中的InvalidArgumentException:未找到View [Site.create].
FileViewFinder中的FileViewFinder.php第137行 -
> 在FileViewFinder
中的FileViewFinder.php第79行中的findInPaths('Site.create',array('/....../ resources/views')) -
找到('Site.在Factory.php第151行中创建')
我尝试工匠的命令:cache:clear,route:clear,config:clear,config:cache和nothings works,我不知道问题出在哪里!
在localhost上它完美无缺
我有这样的路线:
Route::post('login', [
'uses' => 'AuthController@postLogin',
'before' => 'guest'
]);
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我只有错误:'类 Illuminate\Routing\Route 的对象无法转换为 int'
我不知道我做错了什么。
我的路线:
Route::group(['middleware' => ['auth, guest']], function () {
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getIndex'));
Route::get('/login', array('as' => 'login', 'uses' => 'AuthController@getLogin')) -
Route::post('login', [ 'uses' => 'AuthController@postLogin', 'before' => 'guest', ]);
});
Run Code Online (Sandbox Code Playgroud)
身份验证控制器
public function postLogin() {
$rules = array('username' => 'required', 'password' => 'required');
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::route('login')->withErrors($validator);
}
$auth = Auth::attempt(array( 'name' => Input::get('username'), 'password' => Input::get('password'), …Run Code Online (Sandbox Code Playgroud) 我已经定义了与投资组合的用户关系,但它让我的用户模型为null
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function portfolio()
{
$this->hasMany('App\Portfolio');
}
}
Run Code Online (Sandbox Code Playgroud)
我的投资组合模型是
class Portfolio extends Model
{
protected $fillable = ['user_id', 'ptitle', 'pdate','pedate','purl','languages','pdes','attachments'];
public function user()
{
$this->belongsTo('App\User');
}
public function attachments()
{
$this->hasMany('App\Attachment');
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在迁移中定义了外键,我的迁移代码是
public function up()
{
Schema::create('portfolios', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('ptitle');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
}); …Run Code Online (Sandbox Code Playgroud)