İyi forumlar.
Sizin için hazırlamış olduğum Laravel URL Kısaltma Script'ini yayınlayacağım.
Öncelikle Routes Ayarlayalım.
Laravelin kurulu olduğu dizinden routes.php yi açın ve aşağıdaki gibi değiştirin.
<?php
Route::get('/', array(
'as' => 'home',
'uses' => 'HomeController@index'
));
Route::post('/make',array(
'as' => 'make',
'uses' => 'LinkController@make'
));
Route::get('/{code}', array(
'as' => 'get',
'uses' => 'LinkController@get'
));
?>Laravelin kurulu olduğu dizinden controllers klasörüne girin.
HomeController.php yi aşağıdaki gibi düzenleyin.
<?php
class HomeController extends BaseController {
public function index()
{
return View::make('home');
}
}
DB yayınlayayım.
# DB
CREATE TABLE IF NOT EXISTS `links` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`code` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10000000 ;
# DB
Laravelin kurulu olduğu dizinden app klasörüne sonra da views klasörüne girin.
home.php dosyasını oluşturun.
views/home.php
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>URL Shorter</title>
<link rel="stylesheet" href="<?php echo URL::to('css/global.css'); ?>" />
</head>
<body>
<div class="container">
<h1 class="title">URL Kısalt</h1>
<?php
if(@$errors->has('url')){
echo '<p>'.$errors->first('url').'</p>';
}
if(Session::has('global')){
echo '<p>'.Session::get('global').'</p>';
}
?>
<form action="<?php echo URL::action('make'); ?>" method="post">
<input type="url" name="url" placeholder="URL Giriniz." autocomplete="off" <?php echo(Input::old('url')) ? 'value="'. e(Input::old('url')) .'"' : '' ?> />
<input type="submit" value="Kısalt" />
</form>
</div>
</body>
</html>Laravelin kurulu olduğu dizinden app klasörüne sonra da models klasörüne girin.
Link.php dosyasını oluşturun.
models/Link.php
class Link extends Eloquent{
protected $fillable = array('url', 'code');
}Laravelin kurulu olduğu dizinden app klasörüne sonra da controllers klasörüne girin.
LinkController.php dosyasını oluşturun.
controllers/LinkController.php
<?php
class LinkController extends BaseController{
public function make(){
$validator = Validator::make(Input::all(),array(
'url' => 'required|url|max:255'
));
if($validator->fails()){
return Redirect::action('home')->withInput()->withErrors($validator);
}else{
$url = Input::get('url');
$code = null;
$exist = Link::where('url', '=', $url);
if($exist->count() == 1){
$code = $exist->first()->code;
}else{
$created = Link::create(array(
'url' => $url
));
if($created){
$code = base_convert($created->id, 10, 36);
Link::where('id', '=', $created->id)->update(array(
'code' => $code
));
}
}
if($code){
return Redirect::action('home')->with('global', 'URL kısaltıldı. <br> URL : <a href="' . URL::action('get',$code) .'">' . URL::action('get', $code) .'</a>');
}
}
return Redirect::action('home')->with('global', 'Hata Oluştu. Tekrar deneyiniz.');
}
public function get($code){
$link = Link::where('code','=',$code);
if($link->count()==1){
return Redirect::to($link->first()->url);
}else{
return Redirect::action('home');
}
}
}
?>Umarım yararım olmuştur.
Kaynak: https://www.youtube.com/playlist?list=PLfdtiltiRHWEhJJgooJ8y_Bbiv2Zb7X6n