This video will help you to work correctly with Laravel ViewComposer
Thank laracasts.com for sharing
https://laracasts.com/series/laravel-5-fundamentals/episodes/25
$result = File::makeDirectory('/path/to/directory');
$result = File::makeDirectory('/path/to/directory', 0775, true);
<?php //file: MyTestComposer.php2. Tạo template để xử lý dữ liệu mà ViewComposer đẩy ra
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class MyTestComposer {
// protected $param;
/**
* Create a new composer.
* @return void
*/
public function __construct() {
// Dependencies automatically resolved by service container...
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view) {
// Code here
$view->with('testVariable', 'Value from MyTest Composer');
}
}
MyComposer Content <br />3. Định nghĩa ComposerServiceProvider
{{ $testVariable }} -- Hien thi gia tri bien $testVariable
View::composer(['frontend.home', 'frontend.newsDetail'], 'App\Http\ViewComposers\MyTestComposer');
<?php // file: ComposerServiceProvider.php4.Sau khi định nghĩa ComposerServiceProvider bạn cần khai báo vào mảng providers để hệ thống thực thi.
namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
/**
* Register bindings in the container.
*
* @return void
*/
public function boot() {
// Using class based composers...
View::composer('frontend.layout', 'App\Http\ViewComposers\MyTestComposer');
// Using Closure based composers...
// View::composer('frontend.layout', function()
// {
// $menu = 'A|B|C';
// $view->with('menus', $menus);
// });
}
/**
* Register
*
* @return void
*/
public function register() {
//
}
}
'providers' => [5. Include template
/*
* Laravel Framework Service Providers...
*/
...... Default Providers
/*
* Application Service Providers...
*/
...... Default Providers
'App\Providers\ComposerServiceProvider'
],
@include('frontend.default.myComposer')6. Done.
return response()->json(['name' => 'Steve', 'state' => 'CA']);
Creating A JSONP Responsereturn response()->json(['name' => 'Steve', 'state' => 'CA'])
->setCallback($request->input('callback'));
Creating A File Download Responsereturn response()->download($pathToFile);
return response()->download($pathToFile, $name, $headers);
$input = Request::only('username', 'password');
$input = Request::except('credit_card');
Hàm làm việc với 1 mảng các inputs: sử dụng dấu chấm "."$input = Request::input('products.0.name');
Old input: Giữ lại input cho tới request tiếp theo (giống FLASH của symfony)Request::flashOnly('username', 'email');
Request::flashExcept('password');
return redirect('form')->withInput();
return redirect('form')->withInput(Request::except('password'));
$username = Request::old('username');
$uri = Request::path();$url = Request::url();
if (Request::is('admin/*'))
{
//
}
Route::post('foo/bar', function()
{
return 'Hello World';
});
Route::put('foo/bar', function()
{
//
});
Route::delete('foo/bar', function()
{
//
});
Regular Expression Parameter ConstraintsRoute::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
Route::get('user/{id}', function($id)
{
//
})
->where('id', '[0-9]+');
Passing An Array Of ConstraintsRoute::get('user/{id}/{name}', function($id, $name)
{
//
})
->where(['id' => '[0-9]+', 'name' => '[a-z]+'])
Defining Global Patterns$router->pattern('id', '[0-9]+');
Route::get('user/{id}', function($id)
{
// Only called if {id} is numeric.
});
Khai báo tên cho Routing, tiện lợi cho việc generate ra URL. Route::get('user/profile', ['as' => 'profile', 'uses' => 'UserController@showProfile']);
$url = route('profile');
$redirect = redirect()->route('profile');
$users = DB::table('users')->remember(10)->get();
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
Left Join StatementDB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
Advanced join clausesDB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
Câu lệnh truy vấn sẽ như sau: select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
select * from users
where exists (
select 1 from orders where orders.user_id = users.id
)
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
'mysql' => array( 'read' => array( 'host' => '192.168.1.1', ), 'write' => array( 'host' => '196.168.1.2', ), 'driver' => 'mysql', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),