- Todos los archivos se deben considerar en el contexto de un proyecto de Laravel
- El nombre del archivo indica su ubicación en el proyecto, usando dos guiones bajos. Por ejemplo: routes__web.php indica que se trata del archivo web.php ubicado en el directorio routes del proyecto
Created
May 21, 2025 12:22
-
-
Save kurotori/18493968924e15319b942430cfb3ec56 to your computer and use it in GitHub Desktop.
Laravel: Rutas y Acceso a funciones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Este archivo debe crearse mediante el comando --> php artisan make:controller LibroController | |
<?php | |
namespace App\Http\Controllers; | |
use App\Models\Libro; | |
use Illuminate\Http\Request; | |
class LibroController extends Controller | |
{ | |
// | |
public function libroNuevo(Request $solicitud){ | |
$libro = Libro::create( | |
[ | |
'isbn' => $solicitud->isbn, | |
'titulo' => $solicitud->titulo, | |
'autor' => $solicitud->autor, | |
'genero' => $solicitud->genero | |
] | |
); | |
return response()->json([ | |
'mensaje'=>'OK', | |
'libro'=>$libro | |
]); | |
} | |
/** | |
* Permite ver todos los libros | |
*/ | |
public function verTodos(){ | |
$libros = Libro::all(); | |
return response()->json( | |
[ | |
'libros'=>$libros | |
] | |
); | |
} | |
/** | |
* Permite realizar una búsqueda general entre los libros registrados | |
*/ | |
public function buscarLibro(Request $solicitud){ | |
$dato = '%'.$solicitud->dato.'%'; | |
$libros = Libro::where('titulo','LIKE',$dato) | |
->orWhere('isbn','LIKE',$dato) | |
->orWhere('autor','LIKE',$dato) | |
->orWhere('genero','LIKE',$dato) | |
->get(); | |
return response()->json( | |
[ | |
'mensaje'=>'OK', | |
'libros'=>$libros | |
] | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Este archivo DEBE crearse con la orden --> php artisan make:model -m Libros | |
//Este comando crea tanto la migraciòn como el modelo 'Libros' | |
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Libro extends Model | |
{ | |
// | |
protected $table = "libros"; | |
protected $fillable = [ | |
'isbn', | |
'titulo', | |
'autor', | |
'genero' | |
]; | |
protected $hidden = [ | |
'id', | |
'created_at', | |
'updated_at' | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Este archivo DEBE crearse con la orden --> php artisan make:model -m Libros | |
//Este comando crea tanto la migraciòn como el modelo 'Libros' | |
//Una vez definida la migración, se debe ejecutar el comando --> php artisan migrate | |
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::create('libros', function (Blueprint $table) { | |
$table->id(); | |
$table->timestamps(); | |
$table->integer('isbn')->unique(); | |
$table->string('titulo',30); | |
$table->string('autor',120); | |
$table->string('genero',30); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('libros'); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Este archivo debe crearse mediante el comando: php artisan make:view agregarLibro | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Agregar Libro</title> | |
</head> | |
<body> | |
<h1>Nuevo Libro</h1> | |
<form action="/libro/nuevo" method="post"> | |
@csrf | |
{{-- (label+input+br)*4 --}} | |
<label for="isbn">ISBN:</label> | |
<input id="isbn" name="isbn" type="text"> | |
<br> | |
<label for="titulo">Título:</label> | |
<input name="titulo" id="titulo" type="text"> | |
<br> | |
<label for="autor">Autor:</label> | |
<input name="autor" id="autor" type="text"> | |
<br> | |
<label for="genero">Género:</label> | |
<input name="genero" id="genero" type="text"> | |
<br> | |
<button type="submit">Agregar Libro</button> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Http\Controllers\LibroController; | |
use Illuminate\Support\Facades\Route; | |
//Ruta incluída por descarte por Laravel. Puede borrarse | |
Route::get('/', function () { | |
return view('welcome'); | |
}); | |
//Ruta para VER el formulario para agregar libros nuevos | |
Route::get( | |
'/libro/ver/agregar', | |
function(){ | |
return view('agregarLibro'); | |
} | |
); | |
//Ruta para visualizar la pá | |
Route::get( | |
'/libro/ver/busqueda', | |
function(){ | |
return view('buscarLibro'); | |
} | |
); | |
//Ruta para ver los datos de TODOS los libros | |
Route::get( | |
'/libro/buscar/todos', | |
[LibroController::class,'verTodos'] | |
); | |
//Ruta para enviar una solicitud de búsqueda de libros | |
Route::get( | |
'/libro/buscar/{dato}', | |
[LibroController::class,'buscarLibro'] | |
); | |
//Ruta para agregar libros | |
Route::post( | |
'/libro/nuevo', | |
[LibroController::class,'libroNuevo'] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment